gpt4 book ai didi

c - 当某些字段为空时,如何使用 sscanf 和 scanset 以逗号分隔的字符串提取字段

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:00 24 4
gpt4 key购买 nike

如果所有字段都已填充,我可以使用 sscanf(见下文)以逗号分隔格式提取字段。但是,如果一个字段是空白的,那么只会填充空白字段。有什么方法可以继续,忽略空白字段的问题,以便填充后续字段吗?

#include <stdio.h>

int main(int argc, char* argv[]) {

char* s = "Apple,Pear,Potato,11";

char fruit1[10];
char fruit2[10];
char vegetable[10];
int size;


int num = sscanf(s, "%20[^,],%20[^,],%20[^,],%d", fruit1, fruit2, vegetable, &size);
if (num == 4) {
printf("This record contains 4 items\n");
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);
}
else {
printf("This record does not contain 4 items\n");
}


// but here it fails - blank vegetable
char* t = "Plum,Orange,,12";
num = sscanf(t, "%20[^,],%20[^,],%20[^,],%d", fruit1, fruit2, vegetable, &size);

if (num == 4) {
printf("This record contains 4 items\n");
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);
}
else {
printf("This record does not contain 4 items\n");
}

return 0;
}


/*
Prints:
This record contains 4 items
first fruit: Apple, second fruit: Pear, vegetable: Potato, size = 11
This record does not contain 4 items
*/

最佳答案

您可以使用strchr构建您自己的扫描函数,注意scan,替换为\0原始字符串,所以如果你想标记一个字符串文字(只读)你需要一个中间缓冲区:

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

char *scan(char **pp, char c)
{
char *s = *pp, *p;

p = strchr(*pp, c);
if (p) *p++ = '\0';
*pp = p;
return s;
}

int main(void)
{
char s1[] = "Apple,Pear,Potato,11";
char s2[] = "Plum,Orange,,12";
char fruit1[10];
char fruit2[10];
char vegetable[10];
int size;
char *p;

p = s1;
strcpy(fruit1, scan(&p, ','));
strcpy(fruit2, scan(&p, ','));
strcpy(vegetable, scan(&p, ','));
size = strtol(scan(&p, ','), NULL, 10);
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);

p = s2;
strcpy(fruit1, scan(&p, ','));
strcpy(fruit2, scan(&p, ','));
strcpy(vegetable, scan(&p, ','));
size = strtol(scan(&p, ','), NULL, 10);
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);

return 0;
}

您可以使用指针简化您的代码:

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

char *scan(char **pp, char c)
{
char *s = *pp, *p;

p = strchr(*pp, c);
if (p) *p++ = '\0';
*pp = p;
return s;
}

int main(void)
{
char s1[] = "Apple,Pear,Potato,11";
char s2[] = "Plum,Orange,,12";
char *v[4], *p;
int i;

p = s1;
for (i = 0; i < 4; i++) v[i] = scan(&p, ',');
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", v[0], v[1], v[2], atoi(v[3]));

p = s2;
for (i = 0; i < 4; i++) v[i] = scan(&p, ',');
printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", v[0], v[1], v[2], atoi(v[3]));

return 0;
}

关于c - 当某些字段为空时,如何使用 sscanf 和 scanset 以逗号分隔的字符串提取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973130/

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