-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