gpt4 book ai didi

c: fscanf 问题包括换行符

转载 作者:行者123 更新时间:2023-11-30 14:22:55 27 4
gpt4 key购买 nike

我正在从文件中读取此文本:

file1: file2,file3,file4
file10: testfile.h
file1: file9

并拆分它

while(fscanf(fp,"%[^:]: %s",map[i].name, map[i].filesNeeded) == 2)
{
printf("%s %s",map[i].name, map[i].filesNeeded);
i++;
}

问题是第二个和第三个变量是用前导换行符保存的:

Details:{name = "\\nfile10", '\\000' <repeats 56 times>, filesNeeded = "testfile.h", '\\000' <repeat

如何在没有前导换行符的情况下保存它?

最佳答案

“%[^:]:%s”添加空格

while(fscanf(fp," %[^:]: %s ",map[i].name, map[i].filesNeeded) == 2)

scanf中的空格吸收空格字符、制表符、换行符

关于c: fscanf 问题包括换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13357717/

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