Home > Net >  pointer in binary files after using fread command
pointer in binary files after using fread command

Time:07-24

im learning at the moment about file pointers,and came across this code which was given as exemple i tried replicating it in visual studio but i keep getting run time errors that are not relevent at this moment

void main()
{ 
   FILE *cfPtr; /* credit.dat file pointer */
   /* create clientData with default information */
   struct clientData client = { 0, "", "", 0.0 };
   if ( ( cfPtr = fopen( "credit.dat", "rb" ) ) == NULL ) 
      printf( "File could not be opened.\n" );
   else { 
      printf( "%-6s%-16s%-11ss\n", "Acct", "Last Name",
         "First Name", "Balance" );
/* read all records from file (until eof) */
      fread( &client, sizeof( struct clientData ), 1, cfPtr );
      while ( !feof( cfPtr ) ) { 
         /* display record */
         if ( client.acctNum != 0 ) 
            printf( "%-6d%-16s%-11s.2f\n", 
               client.acctNum, client.lastName, 
               client.firstName, client.balance );
         fread( &client, sizeof( struct clientData ), 1, cfPtr );
      } /* end while */
      fclose( cfPtr ); /* fclose closes the file */
   } /* end else */
} /* end main */

my question is this,if the file is empty what does struct client contains? also if the file only have 1 struct when in the code they used fread wouldnt the pointer move to after the struct meaning it will sit on eof and when they used if it will be false meaning the struct wasnt printed on screen?

CodePudding user response:

my question is this,if the file is empty what does struct client contains? also if the file only have 1 struct when in the code they used fread wouldnt the pointer move to after the struct meaning it will sit on eof and when they used if it will be false meaning the struct wasnt printed on screen?

7.21.8.1 The fread function
...
Synopsis

1
#include <stdio.h>
size_t fread(void * restrict ptr,
     size_t size, size_t nmemb,
     FILE * restrict stream);
Description

2 The fread function reads, into the array pointed to by ptr, up to nmemb elements whose size is specified by size, from the stream pointed to by stream. For each object, size calls are made to the fgetc function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object. The file position indicator for the stream (if defined) is advanced by the number of characters successfully read. If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial element is read, its value is indeterminate.

Returns

3 The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file is encountered. If size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged.
C 2011 Online Draft

Emphasis added; if fread returns something less than 1 for this code, then you should assume that client doesn't contain anything meaningful.

Never use feof as your loop condition - it won't return true until you try to read past the end of the file, so the loop will execute once too often. Instead, check the result of your input operation like so:

while ( fread( &client, sizeof client, 1, cfPtr ) == 1 )
{
  // process client normally
}

if ( feof( cfPtr ) )
  fputs( "End of file detected on input\n", stderr );
else
  fputs( "Error on input\n", stderr );
  • Related