C getline()函数无法按预期工作

C++ getline() function not working as intended

本文关键字:工作 getline 函数      更新时间:2023-10-16
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 s," 10 11"。

我正在返回Lineerr。我似乎无法弄清楚为什么我的getline()函数返回false。

写入output流后,您就位于流的末尾。要再次读取数据,您需要寻找流的开始:

output.seekg(0, ios_base::beg);

顺便说一句,output是您正在阅读的流的非常糟糕的名字。: - )