gpt4 book ai didi

c - 在 C 中从文件中读取逗号分隔值

转载 作者:行者123 更新时间:2023-11-30 14:52:41 25 4
gpt4 key购买 nike

我想读这个

                zeyad,aar,eee,100,sss,55,science
toto,art,bb,100,ss,55,drawing

从文件中获取,然后将其存储在 books.title、books.publisher 等结构中,如下所示

有人可以告诉我如何不读取逗号并仅将字符串存储在其位置吗?我尝试在每个字符串之间使用 %*C 但它不起作用。

while (!feof(pBook))
{
fscanf(pBook,"%s%*c%s%*c%s%*c%s%*c%s%*c%d%*c%s",
books[z].Title,books[z].Author,books[z].Publisher,books[z].ISBN,books[z].DOP,
&books[z].Copies,books[z].Category);
fscanf(pBook,"\n");
z++;
}
fclose(pBook);

最佳答案

一个简单的例子(未经测试):

void SeparateCommas(char *FileName)
{
FILE *fd = fopen(FileName, "r");
size_t len = 0;
ssize_t read;
char *line = NULL;
char temp[50][32];
char *token;
char *end_str;

while((read = getline(&line, &len, fd)) != -1)
{
printf("Read line: %s", line);
token = strtok_r(line, ",", &end_str);

while(token != NULL)
{
strncpy(temp[i], token, sizeof(temp[i]));
printf("Read word: %s", temp[i]);
token = strtok_r(NULL, ",", &end_str);
i++;
}
}
}

关于c - 在 C 中从文件中读取逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596875/

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