From the documentation:
If the input sequence runs out of characters to extract (i.e., the end-of-file is reached) before
n
characters have been successfully read, the array pointed to bys
contains all the characters read until that point, and both theeofbit
andfailbit
flags are set for the stream.
…
The number of characters successfully read and stored by this function can be accessed by calling membergcount
.
So you can do:
while(in_file.read(buffer, buffer_length)){
temp = sendto(sender_socket, buffer, sizeof(buffer), 0, NULL, 0);
byte_count += temp;
msg_count ++;
}
streamcount n = in_file.gcount();
if (n > 0) { // read a partial buffer
temp = sendto(sender_socket, buffer, n, 0, NULL, 0);
byte_count += temp;
msg_count++;
}
3
solved fstream read behavior upon hitting eof