目標是讀取以下文件中的每個整數並將它們全部添加。但是由於某種原因,我似乎無法將字符串行轉換爲int。 代碼:FStream - 讀取文件內部的整數C++
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string line;
ifstream file ("Random.txt");
int lines;
int amount = 0;
while(getline(file, line)){
lines++;
amount += static_cast<int>(line);
}
cout << amount;
return 0;
}
txt文件:
2
3
4
6
任何幫助,將不勝感激
感謝但我得到這個錯誤:[錯誤]「Stoi旅館」不是「性病」 –
成員@TylerEsposito你有沒有'#include'? –
更好地直接使用'ifstream'來讀取'int'。 – JSQuareD