-1
我正在尝试读取文件“Numbers.txt”,但无法访问它。我的代码有什么问题?我提供Numbers.txt
存在于同一个目录下的可执行文件,并Numbers.txt
文件包含每行一个整数无法读取文件
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int sum = 0;
int x;
ifstream inFile;
inFile.open("Numbers.txt");
if (!inFile)
{
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> x) {
sum = sum + x;
}
inFile.close();
cout << "Sum = " << sum << endl;
return 0;
}
呃...文件不存在?我们不能从这里知道... –
你会得到什么错误? – Cuber
我收到“无法打开文件”消息。它没有找到我的文件。我相信它在同一个目录中。它在同一个文件夹中(对不起,我对此很新),并且据我所知它应该能够读取它。 – erehmann