0
我試圖讀取.txt文件中的行並使用std::cout
將其打印出來。我的代碼發佈在下面,但目前沒有在while循環中傳遞任何行到向量。我哪裏做錯了?從文件中獲取getline的問題
int main(int argc, const char * argv[]) {
std::ifstream input("input1.txt");//(argv[1]);
if (!input.good()) {
cerr << "Can not open the grades file "; //<< argv[1] << "\n";
return 1;
}
std::vector<string> art;
string x; // Input variable
// Read the scores, appending each to the end of the vector
cout << "Start While:"; //For Debugging Purposes
while (getline(input, x)) {
art.push_back(x);
}
for (int i=0; i<art.size(); i++) {
cout << art[i] << "\n";
}
return 0;
}
文件中是否有行?代碼對我來說看起來很好。 – Barry 2015-01-31 19:47:45
驗證確實w/VS2013.4 – 2015-01-31 20:01:14
我對這段代碼看到的唯一奇怪之處是混合使用和不使用合格的'std'命名空間(你應該更喜歡「use」側,btw)。否則代碼看起來合理,應該在包含數據行的文件上打開'input'。 – WhozCraig 2015-01-31 20:04:29