C语言 从 C 文件中读取整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12944758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading integers from a file in C
提问by crazymao
I've been struggling with this for a really long time trying to figure it out, I'm guessing it's a really stupid noob mistake but I can't figure it out.
我一直在努力解决这个问题很长时间试图弄清楚,我猜这是一个非常愚蠢的菜鸟错误,但我无法弄清楚。
So I'm trying to read in an integer from a file and then do some operations in it, for the sake of the problem I'm just trying to output it here:
所以我试图从文件中读取一个整数,然后在其中执行一些操作,为了解决这个问题,我只是想在这里输出它:
FILE * pFile;
int number;
pFile = fopen ("myfile.txt","r");
if (pFile!=NULL) {
fscanf(pFile, "%d", number);
fclose (pFile);
}
printf("number: %i", number);
return 0;
the contents of myfile.txt:
myfile.txt 的内容:
123
but when I run it it doesnt read it anything instead it says
但是当我运行它时,它没有读取任何内容,而是说
RUN FAILED (exit value 1, total time: 32ms)
Thanks for any and all help
感谢您的任何帮助
EDIT: I forgot to mention, it works as a string, it reads the correct number, but I can't do any operations on it, also if I set the number type to double it works but outputs some random number in the millions...
编辑:我忘了说,它作为一个字符串工作,它读取正确的数字,但我不能对它做任何操作,如果我将数字类型设置为双倍它也能工作,但输出一些随机数,数百万。 ..
回答by Musa
You need to pass the address of the int variable to fscanf
您需要将 int 变量的地址传递给 fscanf
fscanf(pFile, "%d", &number);
回答by Ed Heal
Yor need to have a pointer to number
你需要有一个指向数字的指针
i.e.
IE
fscanf(pFile, "%d", &number);
And it is a good idea to check the return value from fscanf
检查返回值是一个好主意 fscanf
You also probably need to do a flush.
您可能还需要进行冲洗。
i.e. After printf add the line
即在 printf 添加行之后
fflush(stdout);
Also you either need to initialise numberor do a returnif you are unable to open the file.
如果您无法打开文件,您也需要初始化number或执行return。
So in summary the code should look like this
所以总而言之,代码应该是这样的
FILE * pFile;
int number;
pFile = fopen ("myfile.txt","r");
if (NULL == pFile && 1 == fscanf(pFile, "%d", &number))
{
printf("Number: %i", number);
fflush(stdout);
return 0;
}
else
{
return -1;
}
回答by Michael
You forgot to pass fscanf the address of number rather than the value of number. So replace
您忘记向 fscanf 传递 number 的地址而不是 number 的值。所以更换
fscanf(pFile, "%d", number);
with
和
fscanf(pFile, "%d", &number);
It is necessary to pass it the address of/ a pointer to number, because otherwise it cannot modify the contents of number.
有必要给它传递一个指向number的地址/指针,否则它无法修改number的内容。
回答by Javad Alizadeh
Never forget that for inputs (from keyboard/file) you need to mention the address of the variable. So you have to put &before the variable.
永远不要忘记,对于输入(来自键盘/文件),您需要提及变量的地址。所以你必须放在&变量之前。
In your case:
在你的情况下:
fscanf(pFile, "%d", &number);
Also you'd better check if you reached the End-of-File or not:
此外,您最好检查是否到达文件尾:
while(fscanf(pFile, "%d", &number) != EOF){
//do the logic
}
Check these manuals for more:
查看这些手册了解更多信息:

