【发布时间】:2014-03-29 00:28:35
【问题描述】:
因此,我目前的代码出现分段错误,并试图缩小可能的范围。我不确定fgetc 函数的起点是否与fprintf 和scanf 的定位相同。
即,如果我在文件上使用了scanf,然后使用fgetc,它会从一开始就开始,还是会从scanf 停止的地方继续?如果是前者,我将如何操作起点?
//reads the second line of the input file in order to grab the ten numbers in the line
int numRead = fscanf(Data, "\n %d %d %d %d %d %d %d %d %d %d", &zero, &one,
&two, &three, &four, &five, &six, &seven, &eight, &nine);
while(EOF != (c = fgetc(Data)))
{
message[i] = c;
i++;
}
输入文件:
0 1 2 3 4 5 6 7 8 9
6 7 8 0 1 9 5 2 3 4
If I were reincarnated, I'd want to come back a
buzzard. Nothing hates him or envies him or wants
him or needs him. He is never bothered or in
danger, and he can eat anything.
-- Mark Twain
【问题讨论】:
-
它将在 scanf 停止的地方继续。但是:有时 scanf 与 fgetc、fgets 等混合使用时会非常讨厌。无论如何,显示您的代码,没有我们找不到任何段错误。
-
使用 valgrind,它可能会查明您的问题。
-
另外,编译时启用编译器可以提供的所有警告。准确了解它发出警告的原因并解决问题。
-
确保
c是int。你读到的第一个字符可能是nine之后的换行符。