Search code examples
c++filevectorwhile-loopistream

Reading in one byte at a time with .get()


So i'm reading in a input file that contains:

lololololololol

I need to read it in using binary one byte at a time for something I'm doing later on. To do this i'm using get() to read it in then storing it into a char. It seems to be working correctly except for the last char that it reads in. The vector that it is reading into contains:

lololololololol
�

I'm not quite sure what this last value is but it's totally throwing off my finial output. So my question is, is there a reason get() would read in a value or byte from my text document that is not there? Or is it reading in something that I don't know of?

code:

while(istr.good()) {
    temp = istr.get();
    input.push_back(temp);
}

Solution

  • It's reading the EOF (end of file) character. You need to do the check after reading it to avoid it being inserted to the vector:

    while(temp = istr.get(), istr.good()) // comma operator
        input.push_back(temp);
    

    Or you might use the 2nd std::istream_base::get overload and let istr implicitly convert to bool:

    while(istr.get(temp))
        input.push_back(temp);
    

    Or try more advanced approaches. operator>> and std::getline would also work fine for this kind of input.