Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;fscanf%c dosn';我不能正常工作_C_Printf_Scanf - Fatal编程技术网

c++;fscanf%c dosn';我不能正常工作

c++;fscanf%c dosn';我不能正常工作,c,printf,scanf,C,Printf,Scanf,我有一个包含以下内容的输入文件: 123 A 012345 ABC 我的输出是: 123 012345 ABC 我明白了: 10 B 01010112345 ABBA 10 B 01010112345阿巴 但是我不知道为什么。测试fopen()和fscanf()的返回值 实际上,fscanf(fin,“%c”,&c)在您的情况下输入一个字符,该字符是空格(在输入文件的123后面),所以 被处决 l contains 123, d contains (space), w contain

我有一个包含以下内容的输入文件:

123 A 012345 ABC 我的输出是:

123 012345 ABC 我明白了:

10 B 01010112345 ABBA 10 B 01010112345阿巴
但是我不知道为什么。

测试
fopen()
fscanf()
的返回值

实际上,
fscanf(fin,“%c”,&c)
在您的情况下输入一个字符,该字符是空格(在输入文件的123后面),所以

被处决

l contains 123, 
d contains (space), 
w contains "A" (next string after space)
and 
m contains "012345 ABC" (because delimiter is '\n')
所以基本上

fprintf(fout, "%d %c %s %s", l, d, w, m);
将打印预期值。但是我假设值没有存储在您期望的变量中。


一旦您可能错过打印,请检查您的
fprintf
语句
w
,因此
“A”
没有打印

请添加一些正确的格式。请告诉我们您是如何进行输出的。@Codor很抱歉,提交时,看到选项卡丢失,将修复该问题。当以明显的方式扩展为完整程序时,这在OSX上对我有效。需要查看完整的工作示例才能知道这是代码中的错误还是C库中的错误。你能显示输入文件的十六进制转储吗?不,转换规范中的空格使得
scanf()
忽略输入中的空格。哦,是的@pmg,我没有注意到你在Ubuntu 12.04中发布的程序在我的gcc编译器中正常工作
fin = fopen("a.i1", "r");
if (!fin) {
    perror("a.i1");
    exit(EXIT_FAILURE);
}
if (fscanf(fin, "%d %c %s %[^\n]", &l, &d, pk, cd) != 4) {
    fprintf(stderr, "fscanf() did not manage to assign 4 values.\n");
    exit(EXIT_FAILURE);
}
fclose(fin);
fscanf(fin, "%d %c %s %[^\n]", &l, &d, w, m); 
l contains 123, 
d contains (space), 
w contains "A" (next string after space)
and 
m contains "012345 ABC" (because delimiter is '\n')
fprintf(fout, "%d %c %s %s", l, d, w, m);