0
char x[5];

ifstream i("test.txt", std::ifstream::binary);

while (i.read(x, sizeof(x))) { 
    ...
    bzero(x, sizeof(x));
}

If the file byte size is not a multiple of 5, the program fails to read the last chunk. How do I make sure to read that as well?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
ExtremistEnigma
  • 239
  • 3
  • 12
  • 2
    If end of file condition occurs on the input sequence (in which case, setstate(failbit|eofbit) is called). The number of successfully extracted characters can be queried using gcount(). http://en.cppreference.com/w/cpp/io/basic_istream/read – Richard Critten Mar 10 '16 at 00:23
  • How do I incorporate that in here? Do I call i.gcount() inside the while block? – ExtremistEnigma Mar 10 '16 at 00:34

1 Answers1

0

what about this:

std::ifstream fin("test.txt", std::ifstream::binary);
std::vector<char> x(5, 0);

while (fin.read(x.data(), x.size())) 
{
    std::streamsize s = fin.gcount();

    // do something with x's data
}
Zoltán
  • 678
  • 4
  • 15