从C中的文件读取整数 [英] Reading integers from a file in C

查看:34
本文介绍了从C中的文件读取整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在努力解决这个问题,我猜这是一个非常愚蠢的菜鸟错误,但我无法解决.

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;

myfile.txt的内容:

the contents of myfile.txt:

123

但是当我运行它时,它什么也没读,而是说

but when I run it it doesnt read it anything instead it says

RUN FAILED (exit value 1, total time: 32ms)

感谢所有帮助

我忘了提,它作为字符串工作,它读取正确的数字,但是我无法对其进行任何操作,即使我将数字类型设置为double,它也可以工作,但会在其中输出一些随机数数百万...

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...

推荐答案

您需要将int变量的地址传递给fscanf

You need to pass the address of the int variable to fscanf

fscanf(pFile, "%d", &number);

这篇关于从C中的文件读取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆