无法连接strtok的输出变量. strcat和strtok [英] Cannot concatenate strtok's output variable. strcat and strtok

查看:101
本文介绍了无法连接strtok的输出变量. strcat和strtok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个程序上花费了几个小时,并且花了几个小时在线搜索我的方法的替代方法,并且整夜都遇到崩溃和错误的困扰……

I’ve spent hours on this program and have put several hours online searching for alternatives to my methods and have been plagued with crashes and errors all evening…

我想通过这段代码实现几件事.首先,我将解释我的问题,然后,我将发布代码,最后,我将解释我对该程序的需求.

I have a few things I'd like to achieve with this code. First I’ll explain my problems, then I’ll post the code and finally I’ll explain my need for the program.

程序仅输出单个单词,而连接功能则不执行任何操作.看来应该足够简单来解决...

The program outputs just the single words and the concatenate function does nothing. This seems like it should be simple enough to fix...

我的第一个问题是我似乎无法使连接函数正常工作,我使用了通用的strcat函数,该函数不起作用,我在互联网上也找不到另一个版本(该函数在这里使用,称为"mystrcat").我想让程序读取一个字符串并删除定界符"以创建一个由原始字符串中的每个单词组成的单个字符串.我正在尝试使用strtok和strcat函数.如果有更简单的方法,请大家注意.

My first problem is that I cannot seem to get the concatenate function to work, I used the generic strcat function which didn't work and neither did another version I found on the internet ( that function is used here, it is called "mystrcat" ). I want to have the program read in a string and remove "delimiters" to create a single string comprised of every word within the original string. I am trying to do with strtok and a strcat function. If there is an easier or simpler way PLEASE I’m all ears.

另一个问题,不一定是问题,而是一团糟:main后面的7行.我希望按照以下方式初始化变量:char variable [amt];但是我为strtok找到的代码是使用指针,而strcat函数的代码是使用指针.更好地了解指针&&字符串地址可能会长期帮助我.但是,我想以任何必要的方式摆脱一些限制.我不能有6行仅用于2个变量.当我有10个变量时,我不需要顶部30行…

Another problem, which isn’t necessarily a problem but an ugly mess: the seven lines following main. I’d prefer to initialize my variables as follows: char variable[amt]; but the code I found for strtok was using a pointer and the code for the strcat function was using pointers. A better understanding of pointers && addresses for strings would probably help me out long term. However I would like to get rid of some of those lines by any means necessary. I can’t have 6 lines dedicated to only 2 variables. When I have 10 variables I do not want 30 lines up top…

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *mystrcat(char *output, char *firstptr);

int main() {

char str[] = "now # is the time for all # good men to come to the # aid of their     country";
char delims[] = "# ";
char resultOrig[70];    //was [20];
char firstOrig[20];
//char *result = NULL, *first = NULL;
char result = resultOrig;    //was result = &resultOrig;
char first = firstOrig;    //was   first = &firstOrig;

first = strtok( str, delims );

while( first != NULL ) {
    mystrcat(resultOrig, firstOrig);
    printf( "%s ", first );
    printf("\n %s  this should be the concat\'d string so far\n", resultOrig);
    first = strtok( NULL, delims );

}
system("pause");
return 0;
}

char *mystrcat(char *resultptr, char *firstptr)
{
char *output = resultptr;

while (*output != '\0')
    output++;
while(*firstptr != '\0')
{
    *output = *firstptr;
    output++;
    firstptr++;
}
*output = '\0';

return output;
}

现在这只是一个测试程序,但我打算将其用于文件列表/数据库.我的文件带有下划线,连字符,句号,括号和数字;我想将所有这些都设置为定界符".我打算通过循环,删除一个定界符(每个循环从_到–到.等...),并创建一个字符串,我可能想用空格或句点代替定界符.而且某些文件中已经包含空格以及我想定界"的特殊字符.

This is just a test program right now but I was intending to use this for a list/database of files. My files have underscores, hyphens, periods, parentheses’, and numbers; all of which I would like to set as the "delimiters". I was planning on going thru a loop, where I would delete a delimiter(each loop-thru change from _ to – to . etc…) and create a single string, I may want to replace the delimiters with a space or a period. And some files have spaces in them already along with the special characters I’d like to "delimit".

我打算通过扫描文本文件来完成所有这些操作.在文件中,我的大小也采用以下格式:"2,518,6452".我希望我可以按字母或大小对数据库进行升序或降序排序.这只是一些其他信息,对于我上面的特定问题,可能会有用处.

I’m planning to do all this by means of scanning a text file. Within the file I also have a size in this format: "2,518,6452". I’m hoping I can sort my database alphabetically or by size, ascending or descending. That’s just some additional information which may be useful to know for my specific questions above.

下面,我提供了一些虚构的示例来说明这些名称的显示方式. my_file(2009).ext second.File-group1.extls the.third.file-vol30.lmth

Below I have included some fictional samples of how these names could appear. my_file(2009).ext second.File-group1.extls the.third.file-vol30.lmth

我将这篇文章的重点放在:关于如何使连接功能正常工作或替代strcat和/或strtok的问题.以及寻求帮助来整理不必要或多余的代码.

I am focusing this post on: the question on how to get the concatenate function working or an alternative to strcat and/or strtok. As well as asking for help to unclutter unnecessary or redundant code.

感谢所有帮助,甚至是所有阅读我的帖子的人.

I appreciate all the help and even all those who read through my post.

非常感谢您!

推荐答案

strcat将起作用.不需要mystrcat.可以简化为:

strcat would work if you used first instead of firstOrig in your loop. No need for mystrcat. Can be simplified to:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "now # is the time for all # good men to come to the # aid of their     country";
    char delims[] = "# ";
    char result[100] = "";  /* Original size was too small */
    char* token;
    token = strtok(str, delims);
    while(token != NULL) {
        printf("token = '%s'\n", token);
        strcat(result, token);
        token = strtok(NULL, delims);
    }
    printf("%s\n", result);
    return 0;
}

输出:

token = 'now'
token = 'is'
token = 'the'
token = 'time'
token = 'for'
token = 'all'
token = 'good'
token = 'men'
token = 'to'
token = 'come'
token = 'to'
token = 'the'
token = 'aid'
token = 'of'
token = 'their'
token = 'country'
nowisthetimeforallgoodmentocometotheaidoftheircountry

这篇关于无法连接strtok的输出变量. strcat和strtok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆