U
Urs Thuermann
I want to read blocks of a file in a loop and I am looking for the
most elegant way to do this. Assume there is a function
process_data() that I don't want to call with zero-length data. In C
I write
while ((nbytes = read(fd, buffer, sizeof(buffer))) > 0) {
process_data(buffer, nbytes);
}
AFAICS, in C++ I should use ifstream::read() for reading from the
file, but that method does not return the number of bytes successfully
read. Unfortunately, like feof(fp) in C, the method ifstream::eof()
does signal the EOF state of the file one byte too late, i.e. only
*after* trying to read one more byte after the last one has been read,
instead of before. Therefore, I cannot write
while (!file.eof()) {
file.read(buffer, sizeof(buffer));
nbytes = file.gcount();
process_data(buffer, nbytes);
}
but instead one must write
while (!file.eof()) {
file.read(buffer, sizeof(buffer));
nbytes = file.gcount();
if (nbytes == 0) // or
break; // if (nbytes > 0)
process_data(buffer, nbytes); // process_data(buffer, nbytes);
}
which I find ugly because of the two loop termination conditions in
the first case (left) and because of the two almost identical
conditions and the additional indentation in the second case (right).
Currently, I have the following, which comes closest to what I've
written hundred of times in C, but does not look quite as clean and
simple:
while (file.read(buffer, sizeof(buffer)), (nbytes = file.gcount()) > 0) {
process_data(buffer, nbytes);
}
So, my question is if there is another simple and elegant way to do this.
urs
most elegant way to do this. Assume there is a function
process_data() that I don't want to call with zero-length data. In C
I write
while ((nbytes = read(fd, buffer, sizeof(buffer))) > 0) {
process_data(buffer, nbytes);
}
AFAICS, in C++ I should use ifstream::read() for reading from the
file, but that method does not return the number of bytes successfully
read. Unfortunately, like feof(fp) in C, the method ifstream::eof()
does signal the EOF state of the file one byte too late, i.e. only
*after* trying to read one more byte after the last one has been read,
instead of before. Therefore, I cannot write
while (!file.eof()) {
file.read(buffer, sizeof(buffer));
nbytes = file.gcount();
process_data(buffer, nbytes);
}
but instead one must write
while (!file.eof()) {
file.read(buffer, sizeof(buffer));
nbytes = file.gcount();
if (nbytes == 0) // or
break; // if (nbytes > 0)
process_data(buffer, nbytes); // process_data(buffer, nbytes);
}
which I find ugly because of the two loop termination conditions in
the first case (left) and because of the two almost identical
conditions and the additional indentation in the second case (right).
Currently, I have the following, which comes closest to what I've
written hundred of times in C, but does not look quite as clean and
simple:
while (file.read(buffer, sizeof(buffer)), (nbytes = file.gcount()) > 0) {
process_data(buffer, nbytes);
}
So, my question is if there is another simple and elegant way to do this.
urs