I have a binary file from which I need to read 32 bit patterns- In the event if the EOF is reached such that its less than 32 bits - I need to error out saying unexpected eof else just break. I tried a lot but I am unable to get the incomplete bytes detecting part working. Can someone provide some pointer how to achieve it ? I did thought of exploring one byte at a time byte[0] and evaluating if its EOF but didnt work.
for (;;)
{
bytes_read = fread(buffer, 4, 1, inFile);
if (bytes_read == 1)
{
// perform the task
}
else
{
// detect if its incomplete sequence or EOF and output appropriately
}
}
P.S : Editing fread as per comment -
sizeof(char)
as the number of items to read? – Barmarsizeof(char)
is always 1. EOF token doesn't fit into achar
. You're trying to read more than one char and still comparing if you only read one? – Sami Kuhmonen3D0F
, which is four characters denoting 16 bits or two bytes. Also note thatsizeof (char)
is1
by definition, and it is inappropriate to use it for another reason: the third argument tofread
represents the number of elements to read, of the size given by the second argument.sizeof (char)
isn't a good way to express the concept of "give me one element". – Kaz