c - detect incomplete 32 bit binary data -


i have binary file need read 32 bit patterns- in event if eof reached such less 32 bits - need error out saying unexpected eof else break. tried lot unable incomplete bytes detecting part working. can provide pointer how achieve ? did thought of exploring 1 byte @ time byte[0] , evaluating if eof didnt work.

   (;;)    {      bytes_read = fread(buffer, 4, 1, infile);      if (bytes_read == 1)      {       // perform task      }      else      {          // detect if incomplete sequence or eof , output appropriately      }    } 

p.s : editing fread per comment -

your code tells fread read 1 record, containing 4 bytes, file. if record incomplete, return 0 (0 records read, error). if record missing (at end of file), return 0 (0 records read, normal situation). must adjust code distinguish these cases, having fread return 4 (bytes) instead.

if it's impossible read 4 bytes @ end of file, want fread output less 4. have behavior, should tell fread read in units of 1 byte:

bytes_read = fread(buffer, 1, 4, infile); 

then @ number:

if (bytes_read == 4) {     // perform task } else if (bytes_read > 0) {     // bad file format - not multiple of 4 bytes } else if (bytes_read == 0) {     break; // success } else // bytes_read = -1 {     // general file error } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -