简体   繁体   English

我正在尝试用定界符分割字符串,但是它不起作用,为什么?

[英]I'm trying to split a string by a delimiter but it doesn't work, why?

I wrote below code to readin line by line from stdin ex. 我写了下面的代码从stdin ex逐行读入。

city=Boston;city=New York;city=Chicago\n

and then split each line by ';' 然后用“;”分隔每一行 delimiter and print each record. 定界符并打印每个记录。

But for some reason the "record" pointer comes back always null. 但是由于某种原因,“记录”指针总是返回空值。 Why? 为什么?

    char    del = ';';
    char    input[BUFLEN];

    while(fgets(input, BUFLEN, fp)) {

            input[strlen(input)-1]='\0';
            char* record = strtok(input, &del);

            while(record) {
                    printf("Record: %s\n",record);
                    record = strtok(NULL, &del);
            }
}

strtok needs a string with a nul (zero byte) at the end for its second argument. strtok的第二个参数需要一个结尾为nul(零字节)的字符串。 Change del to be del更改为

 char * del = ";"

Here is a full program (minus the input reading part): 这是完整的程序(减去输入阅读部分):

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

int main ()
{
   char*    del = ";";
   char * input = strdup ("city=Boston;city=New York;city=Chicago\n");
   char* record = strtok (input, del);
   while (record) {
       printf("Record: %s\n",record);
       record = strtok (NULL, del);
   }
}

Try it: http://codepad.org/tzzxjOJE 试试看: http : //codepad.org/tzzxjOJE

There is also a strsep function which has the advantage of handling more than one string at a time. 还有一个strsep函数 ,它的优点是一次处理多个字符串。

Try char * del = ";"; 尝试char * del =“;”;

The second parameter should be a null terminated string, ie an array of chars with a null termination. 第二个参数应该是一个以null结尾的字符串,即一个以null结尾的char数组。

And then pass in del to strtok. 然后将del传递给strtok。

   char*    del = ";";
   char    input[BUFLEN];

    while(fgets(input, BUFLEN, fp)) {

            input[strlen(input)-1]='\0';
            char* record = strtok(input, del);

            while(record) {
                    printf("Record: %s\n",record);
                    record = strtok(NULL, del);
            }
     }

Your call to strtok() is very likely to segfault. 您对strtok()的调用很可能会发生段错误。 The char * pointed to by &del does not have a NULL terminator. &del指向的char *没有NULL终止符。

strtok() expects the delimiters string to be exactly that - a null-terminated C string. strtok()希望定界符字符串恰好是-以空值终止的C字符串。 You're passing it a single character, which obviously isn't a null-terminated string since a semicolon is not a null character. 您正在为它传递一个字符,因为分号不是空字符,所以它显然不是以空字符结尾的字符串。 Instead, try this: 相反,请尝试以下操作:

char del[] = ";";

I'm not sure but I think because your del won't have the proper '\\0' at the end. 我不确定,但我认为是因为您的del末尾没有正确的'\\ 0'。

You have to make sure the string is NULL terminated or most of the string function are gonna read the memory after your variable which could leads to LOTS of problems 您必须确保字符串以NULL终止,否则大多数字符串函数将在变量之后读取内存,这可能会导致很多问题

something like this would be better: 这样的事情会更好:

    char    *del = ";";
    char    input[BUFLEN];

    while(fgets(input, BUFLEN, fp)) {

            input[strlen(input)-1]='\0';
            char* record = strtok(input, del);

            while(record) {
                    printf("Record: %s\n",record);
                    record = strtok(NULL, del);
            }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试扫描一个字符串并将其放入 function,但它不起作用 - I am trying to scanf a string and put it in a function, but it doesn't work 我正在尝试将随机整数分配给struct成员,但无法正常工作 - I'm trying to assign random integers to struct members, but it doesn't work properly 我正在尝试将多个客户端与此服务器连接,但代码似乎不起作用&gt; - I'm trying to connect multiple clients with this server, but the code doesn't seem to work> 我正在尝试使用结构在 c 中创建堆栈,但我的推送 function 不起作用 - I'm trying to create a stack in c using structures but my push function doesn't work 试图用两个定界符分开,但不起作用-C - Trying to split by two delimiters and it doesn't work - C 为什么这个 c++ 代码模板不起作用? 我有麻烦了 - Why doesn't this c++ code template work? I'm having trouble 我正在尝试编写一个C程序来将文件中的整数存储到数组中,但是它不起作用。 有人能帮我吗? - I'm trying to write a C program to store integers from a file into an array, but it doesn't work. Can someone help me? 我正在尝试创建一个数组并用 1 到 10 的数字填充它。为什么它不起作用? - I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work? 为什么在 C 中修改了字符串,即使我没有尝试修改它? - Why is string modified in C even when I'm not trying to modify it? 我正在学习如何将矩阵内的值相加。但是我尝试了两种方法,为什么第二种方法不起作用? - I'm learning how to add the values ​inside a matrix together.But I try two ways, why the second one doesn't work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM