无法连接 strtok 的输出变量。 strcat 和 strtok

标签 c pointers concatenation delimiter strtok

我在这个程序上花了几个小时,并花了几个小时在网上搜索我的方法的替代方案,但整个晚上都被崩溃和错误所困扰……

我有几件事想用这段代码实现。首先我会解释我的问题,然后我会发布代码,最后我会解释我对这个程序的需求。

程序只输出单个单词,连接函数什么也不做。这看起来应该很容易修复......

我的第一个问题是我似乎无法让连接函数工作,我使用了通用的 strcat 函数,但它不起作用,我在互联网上找到的另一个版本也没有(该函数在这里使用,它被称为“mystrcat”)。我想让程序读入一个字符串并删除“分隔符”以创建一个由原始字符串中的每个单词组成的字符串。我正在尝试使用 strtok 和 strcat 函数。如果有更简单或更简单的方法,请我洗耳恭听。

另一个问题,这不一定是问题,而是一个丑陋的困惑:main 之后的七行。我更愿意按如下方式初始化我的变量:char variable[amt];但是我为 strtok 找到的代码使用的是指针,而 strcat 函数的代码使用的是指针。更好地理解字符串的指针 && 地址可能会长期帮助我。但是,我想以任何必要的方式摆脱其中一些行。我不能让 6 行只用于 2 个变量。当我有 10 个变量时,我不希望顶部有 30 行......

#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;
}

这只是一个测试程序,但我打算将其用于文件列表/数据库。我的文件有下划线、连字符、句点、括号和数字;我想将所有这些设置为“分隔符”。我正计划通过一个循环,在那里我将删除一个分隔符(每个循环从 _ 到 – 到 . 等......)并创建一个字符串,我可能想用空格或句点替换分隔符。有些文件中已经有空格以及我想“分隔”的特殊字符。

我打算通过扫描文本文件来完成所有这些工作。在文件中,我还有一个这种格式的大小:“2,518,6452”。我希望我可以按字母顺序或大小、升序或降序对我的数据库进行排序。这只是一些额外的信息,可能有助于了解我上面的具体问题。

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

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

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

非常感谢!

最佳答案

如果您在循环中使用 first 而不是 firstOrig

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

#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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227353/

相关文章:

ubuntu - 我想在 ubuntu 上合并 mp3 文件

c - 在 C 中连接两个字符串的安全方法

c - 这个 reverse() 函数在下面的代码中是如何工作的?

c - 如何在我的 C 卸载函数中调试 "undefined reference"?

c - 使用 Linux 命令将十六进制信息转换为二进制信息

c - 为什么 pcap_next_ex 的第三个参数是不兼容的指针类型?

c++ - 如何在 C(首选)/C++ 中按顺序将一组一维数组传递给函数

读取和存储一系列整数的 C 程序

c++ - int &a = b 在 C 4.8.1 中使 'a' 成为指向 'b' 的指针,但在 C 中却没有,为什么? (按引用调用)

mysql - 使用串联更新 MySQL 表