[英]C++ getline and if/else if statements are not working as I intended
[英]C++ getline() function not working as intended
int boardDim(ifstream & inputFile, unsigned int x, unsigned int y) {
inputFile.open("test.txt");
if (!(inputFile.is_open())) {
throw fileNotOpen;
}
else {
stringstream output;
string output1;
if (getline(inputFile, output1)) {
output << output1;
if (output >> x) {
if (output >> y) {
return success;
}
return secBoardVarErr;
}
return firstBoardVarErr;
}
return lineErr;
}
cout << x << endl;
cout << y << endl;
}
輸入文件包含兩個int
的一行,即“ 10 11”。
我要返回lineErr。 我似乎無法弄清楚為什么我的getline()函數返回false。
寫入output
流后,您就位於流的末尾。 為了能夠再次讀取數據,您需要尋找到流的開頭:
output.seekg(0, ios_base::beg);
順便說一句,對於您正在讀取的流, output
是一個非常不好的名字。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.