lundi 2 janvier 2017

std::ifstream.eof() doesn't work for 0-length files

std::ifstream ifs;
ifs.open("somefile",std::ifstream::in|std::ifstream::binary);
if (!ifs.good()) {
    ifs.close();//required here??
    exit(1);
}
uint8_t * buffer = new uint8_t[length];
do {
    ifs.read(buffer,length);
} while (
//in the mean while, that's the buffer filled with? what about getline?
how about using 
while (!ifs.eof()) {//here look at stored file size and position
    ifs.read(buffer,length);
}

std::ifstream.read() seems poorly implemented - what to do when less than length number of bytes has been read? how do I find out how many bytes was read? otherwise, I can only assume that, even despite sentry etc. that length-size chunks were read when this is not the case. the function should at least have a &bytesRead or &wordsRead parameter (the latter in case they are using wchar_t or u32char_t).

Aucun commentaire:

Enregistrer un commentaire