gpt4 book ai didi

c - 无法找到空标记的空 csv 值的值

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:43 25 4
gpt4 key购买 nike

所以我想创建一个 C 程序来读取一个可能包含一些空值的 csv 文件。如果有的话,我需要我的程序仍然将标记设置为空值。我编写这段代码是因为 strtok() 会简单地忽略所有空值。

该程序的工作原理是获取该记录字符串,并通过这些 if 检查将其分成 3 个标记。

char record[100] = "1,,3,";

char delimiter[] = ",";
char *token1 = 0;
char *token2 = 0;
char *token3 = 0;



static char *stringtobetokened = NULL;
char *p= 0;
stringtobetokened = record;

if ((p = strpbrk(stringtobetokened, delimiter)) != NULL) {
*p = 0;
token1 = stringtobetokened;
stringtobetokened = ++p;
printf("token1's value:%s\n", token1);

}

if ((p = strpbrk(stringtobetokened, delimiter)) != NULL) {
*p = 0;


token2 = stringtobetokened;
stringtobetokened = ++p;
printf("token2's value:%s\n", token2);
//this is where the issue is, this if check should be triggered since token2 is a empty value which should print the statement, token2 is null
if (token2 == NULL)
{
printf("token2 is null\n");
//insert some code that changes token2's value

}

}
if ((p = strpbrk(stringtobetokened, delimiter)) != NULL) {
*p = 0;
token3 = stringtobetokened;
stringtobetokened = ++p;
printf("token3's value:%s\n", token3);

}

我的问题是,虽然它确实发现 token 2 是空的,但 if 检查

if (token2 == NULL)

不触发。我想要这个 if 检查触发,这样我就可以在那里插入一些代码来改变它的值。

如果 token 2 不为 null 或 '\0' 那么它的值是多少??

这些是我运行上述代码时得到的结果:

token1's value:1
token2's value:
token3's value:3
Press any key to continue . . .

最佳答案

token2 将指向 字符串,因为您在之前的标记化过程中将 , 替换为 0

                      token1 ----
|
+---+---+---+-----
stringtobetokened = | 1 | 0 | 0 |........
+---+---+---+-----
|
token2 ----

用以下条件替换您的if检查。

 if (*token2 == '\0')

关于c - 无法找到空标记的空 csv 值的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com