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?
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 functionC 2011 Online Draft
...
Synopsis
1Description#include <stdio.h> size_t fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
2 Thefread
function reads, into the array pointed to byptr
, up tonmemb
elements whose size is specified bysize
, from the stream pointed to bystream
. For each object,size
calls are made to thefgetc
function and the results stored, in the order read, in an array ofunsigned 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 Thefread
function returns the number of elements successfully read, which may be less thannmemb
if a read error or end-of-file is encountered. Ifsize
ornmemb
is zero,fread
returns zero and the contents of the array and the state of the stream remain unchanged.
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 );