Problem with Creating File for File Processing

This is a discussion on Problem with Creating File for File Processing within the C Programming forums, part of the General Programming Boards category; Hello, my problem deals with File Processing. The program I have creates a database and lets me input data for ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    Problem with Creating File for File Processing

    Hello, my problem deals with File Processing. The program I have creates a database and lets me input data for update. I have a problem creating the file correctly.

    I need to keep a database in a binary file and update it everytime the user runs the program. My problem occurs when I run the program multiple times. When I run the program the first time, it runs through both if statements and the file is created; but ends without proceeding to the following else statement. When I run the program again from then on, it runs correctly as intended. My problem lies within the creation of the file. I need to create the file on the first run and have it advance to the else statement so I can proceed through the program; and make sure all of the data is saved into the database for the next time I run the program.

    Example: I run the program and add an axe into the database.
    The next time I run the program, I should have a axe in my database.


    Code:
       FILE *cfPtr;
       int choice;  
       
       if ( ( cfPtr = fopen( "hardware.dat", "rb+" ) ) == NULL ) {
          printf( "File could not be opened. \n" );
          
          if ( ( cfPtr = fopen( "hardware.dat", "wb+" ) ) != NULL ) 
          printf( "Created. \n" );
          
       }
       else { 
            
          while ( ( choice = enterChoice() ) != 5 ) { 
            
             switch ( choice ) { 
                case 1:
                   listInventory( cfPtr );
                   break;
                   
                case 2:
                   updateRecord( cfPtr );
                   break;
                   
                case 3:
                   newRecord( cfPtr );
                   break;
                   
                case 4:
                   deleteRecord( cfPtr );
                   break;
                   
                default:
                   printf( "Incorrect choice \n" );
                   break;
             }
          } 
          fclose( cfPtr );
       }
     system("pause");
       return 0; 
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Remove the else

    Or make it
    if ( cfPtr != NULL )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Thanks alot , i removed the else statement, the program works perfect now

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem creating tmp file in /var/tmp
    By Rahulds in forum Linux Programming
    Replies: 1
    Last Post: 03-14-2009, 01:57 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM

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