使用fscanf()使用C解析逗号分隔文件
我有一个数据类似的文件 –
Name, Age, Occupation John, 14, Student George, 14, Student William, 23, Programmer
现在,我想读取数据,使每个值(例如Name,Age等)作为字符串读取。
这是我的代码片段 –
.... if (!(ferror(input_fp) || ferror(output_fp))) { while(fscanf(input_fp, "%30[^ ,\n\t]%30[^ ,\n\t]%30[^ ,\n\t]", name, age_array, occupation) != EOF){ fprintf(stdout, "%-30s%-30s%-30s\n", name, age_array, occupation); } fclose(input_fp); fclose(output_fp); } ....
然而,这进入无限循环,给出一些随机输出。
这就是我理解input conversion specifiers
。
%30[^ ,\n\t]
– >读取一个长度最多为30个字符的字符串
不包括空格,逗号,换行符或制表符。
我正在阅读3个这样的字符串。
我哪里错了?
OP的
fscanf(input_fp, "%30[^ ,\n\t]%30[^ ,\n\t]%30[^ ,\n\t]", ...
不消耗文本文件中的','
和'\n'
。 随后的fscanf()
尝试也会失败并返回值0(不是EOF
)会导致无限循环。
尽管OP请求了fscanf()
解决方案,但fgets()/sscanf()
更好地处理潜在的IO和解析错误。
FILE *input_fp; FILE *output_fp; char buf[100]; while (fgets(buf, sizeof buf, input_fp) != NULL) { char name[30]; // Insure this size is 1 more than the width in scanf format. char age_array[30]; char occupation[30]; #define VFMT " %29[^ ,\n\t]" int n; // Use to check for trailing junk if (3 == sscanf(buf, VFMT "," VFMT "," VFMT " %n", name, age_array, occupation, &n) && buf[n] == '\0') { // Suspect OP really wants this width to be 1 more if (fprintf(output_fp, "%-30s%-30s%-30s\n", name, age_array, occupation) < 0) break; } else break; // format error } fclose(input_fp); fclose(output_fp);
而不是调用ferror()
,检查fgets()
, fprintf()
返回值。
怀疑OP的未声明的字段缓冲区是[30]
并相应地调整了scanf()
。
[编辑]
关于if (3 == sscanf(buf, VFMT "," ...
详细信息if (3 == sscanf(buf, VFMT "," ...
if (3 == sscanf(...) && buf[n] == '\0') {
在以下情况下变为:
1)确切地说3 "%29[^ ,\n\t]"
格式说明每个scanf至少1个char
。
2) buf[n]
是字符串的结尾。 n
通过"%n"
说明符设置。 前面的' '
in " %n"
会导致最后一个"%29[^ ,\n\t]"
之后的任何后续空格被消耗。 scanf()
看到"%n"
,它指示它设置从扫描开始的当前偏移量,以分配给&n
指向的int
。
"VFMT "," VFMT "," VFMT " %n"
由编译器连接到
" %29[^ ,\n\t], %29[^ ,\n\t], %29[^ ,\n\t] %n"
。
我发现前者比后者更容易维护。
" %29[^ ,\n\t]"
的第一个空格指示sscanf()
扫描(消耗而不是保存)0个或更多个空格( ' '
, '\t'
, '\n'
等等) )。 其余的指示sscanf()
使用并保存除 ','
, '\n'
, '\t'
之外 ','
任何 1到29个char
,然后附加'\0'
。
您没有跳过值之间的实际逗号和空格。
一旦第一个%30[^ ,\n\t]
说明符匹配,输入可能包含一个逗号和一个空格,它们与格式字符串中的以下内容不匹配。
将逗号和空格添加到输入中预期的格式字符串中:
while(fscanf(input_fp, "%30[^ ,\n\t], %30[^ ,\n\t], %30[^ ,\n\t]", name, age_array, occupation) == 3) ^ ^ | | \ / add these to make fscanf() skip them in the input!
此外,检查fscanf()
的返回值是次优的:在依赖已转换的值之前,应检查返回值是否等于转换次数。
另外,你使用反斜杠线延续字符是完全没有意义的,应该删除。