当我使用本书中推荐的代码时,出现错误。我正在使用NetBeans 6.8 for Mac。C++:如何编写一个程序从文件中读取整数?
下面是代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
int number;
inputFile.open("MacintoshHD/Users/moshekwiat/Desktop/random.txt");
inFile >> number;
cout<<endl <<number<<endl;
inputFile.close();
return (0);
}
以下是错误:
main.cpp:20: error: 'inFile' was not declared in this scope
什么需要做的事情? 谢谢
谢谢,摆脱了错误,但现在它只是输出0!该文件在每行上有不同的编号 – mokwi8 2010-07-01 14:59:59
@ mokwi8:如果要读取每行,请这样做。程序中没有循环的痕迹。您在程序中从inFile中读取一次,这可能是发生了什么事情。 – 2010-07-01 15:01:44
@David Thornley:对不起,我是一般的C++ /编程新手。什么是最好的循环语句使用?我不知道有多少数字。 此外,我检查了文件的第一个数字不是0,它是42 – mokwi8 2010-07-01 15:05:15