I'm trying to run this simple program from Deitel's textbook "C How to Program", that scans input from stdin and puts them in a file.
#include <stdio.h>
int main( void )
{
int account; /* account number */
char name[ 30 ]; /* account name */
double balance; /* account balance */
FILE *cfPtr; /* cfPtr = clients.dat file pointer */
/* fopen opens file. Exit program if unable to create file */
if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
} /* end if */
else {
printf( "Enter the account, name, and balance.\n" );
printf( "Enter EOF to end input.\n" );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
/* write account, name and balance into file with fprintf */
while ( !feof( stdin ) ) {
fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
} /* end while */
fclose( cfPtr ); /* fclose closes file */
} /* end else */
return 0; /* indicates successful termination */
} /* end main */
When I enter this input
Enter the account, name, and balance. Enter EOF to end input.
? 100 Jones 24.98
? 200 Doe 345.67
? 300 White 0.00
? 400 Stone -42.16
? 500 Rich 224.62
? EOF
????????????????????..........(cont.)
This is followed by an infinite loop of "?". The clients.dat file is corrupted with "?" characters. What is the problem here?
EDIT
Seems that Ctrl+D on a MacOSX does the trick.