0

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%-11s%10s\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%10.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?

2
  • Now is the time to read the documentation of fread(). What do you get if you call it at the end of the file? Commented Jul 22, 2022 at 15:28
  • The spec tells us "If a partial element is read, its value is unspecified.". But I would argue, it nothing is read at all, the content of the destination buffer is likely to be unchanged, thus your struct client will keep the data from before the call. In general that does not matter at all because if fread did not read as much elements as you requested (check the return value!), you mustn't use the missing entries. Commented Jul 22, 2022 at 15:32

1 Answer 1

0

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 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.