Harrison
Harrison

Reputation: 11

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;
}

The input file contains one line of two ints, "10 11".

I'm returning the lineErr. I can't seem to figure out why my getline() function is returning false.

Upvotes: 0

Views: 88

Answers (1)

Bo Persson
Bo Persson

Reputation: 92261

After you write to the output stream, you are at the end of the stream. To be able to read the data again, you need to seek to the beginning of the stream:

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

BTW, output is a really bad name for a stream you are reading from. :-)

Upvotes: 2

Related Questions