0

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.

Tony54
  • 265
  • 1
  • 4
  • 9
  • Don't use `while(!feof)` . – ameyCU Oct 22 '15 at 04:49
  • 1
    @ameyCU: The best (canonical) question for that is [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Oct 22 '15 at 05:44
  • 1
    When you type `E`, `O`, `F`, things went haywire because `scanf()` is looking for a decimal number, but `E` is not a decimal digit, so the `scanf()` fails, leaving the `E` to be read by the next input operation, which won't be spotting the EOF condition because there's still an `E` (and an `O` and an `F` and a newline) to be read, so it gets back to the `scanf()` which still thinks `E` is not a digit, and … well, it takes a long time before things stop. You normally indicate EOF at a keyboard by typing control-D on Unix and control-Z on Windows — but that is configurable. – Jonathan Leffler Oct 22 '15 at 05:46
  • @JonathanLeffler Yes, that fixed the problem. However can't award you as you didn't put an answer. – Tony54 Oct 22 '15 at 07:21

1 Answers1

0

Please try this, it runs fine. Your code was nearly good, only a small confusion in the loops. I modified the STRICT MINIMUM to get it to run and I left you some work to do. Replaced EOF with 0 , seems simpler, but that's up to you.

#include

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" );
    return ( 0 );
  }             /* end if */
  // else {

  while ( 1 )
  {
    printf( "Enter the account, name, and balance.\n" );
    printf( "Enter 0 to end input.\n" );
    printf( "? " );
    scanf( "%d%s%lf", &account, name, &balance );
    if ( account == 0 )
      break;
    /* 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 */
BobRun
  • 756
  • 5
  • 12