something wrong with my program when I end the file....

This is a discussion on something wrong with my program when I end the file.... within the C Programming forums, part of the General Programming Boards category; I need help...I can create the file...write to the file...but when I end the file...it gives me endless cursors until ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    something wrong with my program when I end the file....

    I need help...I can create the file...write to the file...but when I end the file...it gives me endless cursors until I close the window...when I go to the read program it it gives me endless replays of the last item listed in the file.......



    Code:
    #include <stdio.h>
    int main()
    { 
       int StudentSSN;     /* Social Security Number */
       char StudentName [ 30 ]; /* Name up to 30 characters */
       double StudentAverage;  /* Average Grade */
       FILE *cfPtr;     /* cfPtr = clients.dat file pointer */
       /* fopen opens file. Exit program if unable to create file  */
       if ( ( cfPtr = fopen( "student.dat", "w" ) ) == NULL ) {
          printf( "File could not be opened\n" );
       } /* end if */
       else { 
          printf( "Enter the StudentSSN,StudentName, and STudentAverage.\n" );
          printf( "Enter EOF to end input.\n" );
          printf( "? " );
          scanf( "%d%s%lf", &StudentSSN, StudentName, &StudentAverage );
          /* write Student SSN, Student Name and Student Average into file with fprintf */
          while ( !feof( stdin ) ) { 
             fprintf( cfPtr, "%d %s %.2f\n", StudentSSN, StudentName, StudentAverage );
             printf( "? " );
             scanf( "%d%s%lf", &StudentSSN, StudentName, &StudentAverage );
          } /* end while */
          
          fclose( cfPtr ); /* fclose closes file */
       } /* end else */
       return 0; /* indicates successful termination */
    } /* end main */

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    How do you "end the file"? What do you type at the prompt to generate EOF?

    I'm guessing that your EOF signal - whatever it is - is not being evaluated as true by your feof call. Instead, whatever you've done instead causes the stream to go into an error state because the scanf call fails (it cannot interpret what you've entered). Since the feof call still returns false you continually enter the loop and the scanf keeps failing but you still keep writing to the file in the meantime.

    I'd suggest changing the loop do something more like this:
    Code:
    while (  3 ==  scanf( "%d%s%lf", &StudentSSN, StudentName, &StudentAverage ) )
    {
        fprintf( cfPtr, "%d %s %.2f\n", StudentSSN, StudentName, StudentAverage );
    } /* end while */
    That will continue to loop until an error condition happens, either EOF or failure to convert the proper number of elements.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my program?
    By Farnaz in forum C++ Programming
    Replies: 29
    Last Post: 02-28-2011, 01:18 PM
  2. Something is wrong with my file reverse program
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 05-21-2008, 11:15 AM
  3. What is wrong with this program
    By dals2002 in forum C Programming
    Replies: 6
    Last Post: 02-17-2008, 09:44 PM
  4. Whats wrong with my file reading program?
    By Alphabird32 in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2002, 03:19 AM
  5. Whats wrong with my program?(writing data into a file)
    By Ruflano in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2002, 07:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21