File processing program not workin...

This is a discussion on File processing program not workin... within the C Programming forums, part of the General Programming Boards category; Hi, Below is a program to store structures in a file and can be able to update them. But somehow ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    File processing program not workin...

    Hi,

    Below is a program to store structures in a file and can be able to update them. But somehow the part where the storing information occurs has an error. The user has to type the record number to see if that record contains information, if it has, then the user can't overwrite it unless he deletes the information. But the program doesn't do this, somehow....And i have a seperate function used to create the actual file, and there's a menu for the user to choose what he/she wants to do. I only posted the related code below.

    This is int main():

    Code:
    #include <stdio.h>
    
    int displayMenu( void );
    void listTools( FILE *fPtr );
    void inputRecords( FILE *fPtr );
    void deleteRecord( FILE *fPtr );
    void updateRecord( FILE *fPtr );
    void createFile( void );
    
    struct inventoryItem {
       int recordNum;
       char toolName[ 30 ];
       int quantity;
       double cost;
    };
    
    int main()
    {
       int choice;
       FILE *fPtr;
    
       createFile();
    
       if ( ( fPtr = fopen( "hardware.dat", "r+" ) ) == NULL )
          printf( "File \'hareware.dat\' could not be opened.\n\n" );
       else {
          while ( ( choice = displayMenu() ) != 6 ) {
             switch ( choice ) {
                case 1:
                   listTools( fPtr );
                   break;
                case 2:
                   inputRecords( fPtr );
                   break;
                case 3:
                   deleteRecord( fPtr );
                   break;
                case 4:
                   updateRecord( fPtr );
                   break;
             }
          }
          fclose( fPtr );
       }
       printf( "\n" );
       system("PAUSE");
       return 0;
    }
    This is function to create the file:
    Code:
    void createFile( void )
    {
       FILE *fPtr;
       int i;
       struct inventoryItem item = { 0, "", -1, 0.00 };
    
       if ( ( fPtr = fopen( "hardware.dat", "w" ) ) == NULL )
          printf( "\nFile could not be created.\n" );
       else
          for ( i = 0; i < 100; i++ )
             fwrite( &item, sizeof( struct inventoryItem ), 1, fPtr );
    
       fclose( fPtr );
    }
    Did i create the file the right way?

    This is the function to input data:
    Code:
    void inputRecords( FILE *fPtr )
    {
       struct inventoryItem item;
       int recordNum;
    
       printf( "Enter the record # ( type 0 to end ): " );
       scanf( "%d", &recordNum );
       fseek( fPtr, ( recordNum - 1 ) * sizeof( struct inventoryItem ), SEEK_SET );
       fread( &item, sizeof( struct inventoryItem ), 1, fPtr );
    
       while ( recordNum != 0 ) {
          if ( item.recordNum != 0 )
             printf( "Record already contains information.\n" );
          else {
             printf( "Enter the tool name, quantity and cost\n? " );
             fscanf( stdin, "%s%d%lf", item.toolName, &item.quantity, &item.cost );
             item.recordNum = recordNum;
             fwrite( &item, sizeof( struct inventoryItem ), 1, fPtr );
          }
          printf( "Enter the record # ( type 0 to end ): " );
          scanf( "%d", &recordNum );
          fseek( fPtr, ( recordNum - 1 ) * sizeof( struct inventoryItem ), SEEK_SET );
          fread( &item, sizeof( struct inventoryItem ), 1, fPtr );
       }
    }
    thnx in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void createFile( void )
    {
       FILE *fPtr;
       int i;
       struct inventoryItem item = { 0, "", -1, 0.00 };
    
       if ( ( fPtr = fopen( "hardware.dat", "w" ) ) == NULL )
          printf( "\nFile could not be created.\n" );
       else
          for ( i = 0; i < 100; i++ )
             fwrite( &item, sizeof( struct inventoryItem ), 1, fPtr );
    
       fclose( fPtr );
    }
    This is part of your problem. You should probably make this a binary file. "wb" is what you want.

    The next part is the fact that it doesn't look like you ever reopen this file once it's been created. When you reopen it, you need to make sure and use "rb+" so that you can "update" the file. That is to say: edit it without deleting the entire thing (ie: so you don't truncate it).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    But after i called the function to create the file in line 3 of int main() i opened it by saying
    Code:
    if ( ( fPtr = fopen( "hardware.dat", "r+" ) ) == NULL )
    And i'm only taught to use the following 6 file open modes:
    "w"
    "r"
    "a"
    "w+"
    "r+"
    "a+"

    Pls help thnx

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "b" is binary. Without "b" in there, it is in text mode. If you're using 'fread' or 'fwrite' to write a whole structure, you definately want to use binary mode.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    The following is a program from my deitel C: HOW TO PROGRAM book. It uses "r+" and "w" with binary files. Are you saying that the code dosn't work? Can you try the code out on your computer and give me an answer? Coz there really is an error while running the code on my comp, saying "File could not be opened" ( see code ). Pls help , thnx in advance.

    Code:
    #include <stdio.h>
    
    struct clientData { 
       int acctNum;
       char lastName[ 15 ];
       char firstName[ 10 ];
       double balance;
    };
    
    int enterChoice( void );
    void textFile( FILE * );
    void updateRecord( FILE * );
    void newRecord( FILE * );
    void deleteRecord( FILE * );
    
    int main()
    { 
       FILE *cfPtr;
       int choice;
    
       if ( ( cfPtr = fopen( "credit.dat", "r+" ) ) == NULL )
          printf( "File could not be opened.\n" );
       else { 
    
          while ( ( choice = enterChoice() ) != 5 ) { 
    
             switch ( choice ) { 
                case 1:
                   textFile( cfPtr );
                   break;
                case 2:
                   updateRecord( cfPtr );
                   break;
                case 3:
                   newRecord( cfPtr );
                   break;
                case 4:
                   deleteRecord( cfPtr );
                   break;
             }
          }
    
          fclose( cfPtr );
       }
    
       system( "PAUSE" );
       return 0;
    }
    
    void textFile( FILE *readPtr )
    { 
       FILE *writePtr;
       struct clientData client = { 0, "", "", 0.0 };
    
       if ( ( writePtr = fopen( "accounts.txt", "w" ) ) == NULL )
          printf( "File could not be opened.\n" );
       else { 
          rewind( readPtr );
          fprintf( writePtr, "%-6s%-16s%-11s%10s\n", 
                  "Acct", "Last Name", "First Name","Balance" );
    
          while ( !feof( readPtr ) ) { 
             fread( &client, sizeof( struct clientData ), 1, 
                    readPtr );
    
             if ( client.acctNum != 0 )
                fprintf( writePtr, "%-6d%-16s%-11s%10.2f\n",
                        client.acctNum, client.lastName,
                        client.firstName, client.balance );
          }
    
    	  fclose( writePtr );
       }
    
    }
    
    void updateRecord( FILE *fPtr )
    { 
       int account;
       double transaction;
       struct clientData client = { 0, "", "", 0.0 };
    
       printf( "Enter account to update ( 1 - 100 ): " );
       scanf( "%d", &account );
       fseek( fPtr, 
              ( account - 1 ) * sizeof( struct clientData ), 
              SEEK_SET );
       fread( &client, sizeof( struct clientData ), 1, fPtr );
    
       if ( client.acctNum == 0 )
          printf( "Acount #%d has no information.\n", account );
       else { 
          printf( "%-6d%-16s%-11s%10.2f\n\n", 
                 client.acctNum, client.lastName, 
                 client.firstName, client.balance );
          printf( "Enter charge ( + ) or payment ( - ): " );
          scanf( "%lf", &transaction );
          client.balance += transaction;
          printf( "%-6d%-16s%-11s%10.2f\n", 
                 client.acctNum, client.lastName, 
                 client.firstName, client.balance );
          fseek( fPtr, 
                 ( account - 1 ) * sizeof( struct clientData ), 
                 SEEK_SET );
          fwrite( &client, sizeof( struct clientData ), 1, 
                  fPtr );
       }
    }
    
    void deleteRecord( FILE *fPtr )
    { 
       struct clientData client, 
                         blankClient = { 0, "", "", 0 };
       int accountNum;
    
       printf( "Enter account number to "
               "delete ( 1 - 100 ): " );
       scanf( "%d", &accountNum );
       fseek( fPtr, 
              ( accountNum - 1 ) * sizeof( struct clientData ), 
              SEEK_SET );
       fread( &client, sizeof( struct clientData ), 1, fPtr );
    
       if ( client.acctNum == 0 )
          printf( "Account %d does not exist.\n", accountNum );
       else { 
          fseek( fPtr, 
             ( accountNum - 1 ) * sizeof( struct clientData ), 
             SEEK_SET );
          fwrite( &blankClient, 
                  sizeof( struct clientData ), 1, fPtr );
       }
    }
    
    void newRecord( FILE *fPtr )
    { 
       struct clientData client = { 0, "", "", 0.0 };
       int accountNum;
       printf( "Enter new account number ( 1 - 100 ): " );
       scanf( "%d", &accountNum );
       fseek( fPtr, 
              ( accountNum - 1 ) * sizeof( struct clientData ), 
              SEEK_SET );
       fread( &client, sizeof( struct clientData ), 1, fPtr );
    
       if ( client.acctNum != 0 )
          printf( "Account #%d already contains information.\n",
                 client.acctNum );
       else { 
          printf( "Enter lastname, firstname, balance\n? " );
          scanf( "%s%s%lf", &client.lastName, &client.firstName, 
                &client.balance );
          client.acctNum = accountNum;
          fseek( fPtr, ( client.acctNum - 1 ) * 
                sizeof( struct clientData ), SEEK_SET );
          fwrite( &client, 
                  sizeof( struct clientData ), 1, fPtr );
       }
    }
    
    int enterChoice( void )
    { 
       int menuChoice;
    
       printf( "\nEnter your choice\n"
          "1 - store a formatted text file of acounts called\n"
          "    \"accounts.txt\" for printing\n"
          "2 - update an account\n"
          "3 - add a new account\n"
          "4 - delete an account\n"
          "5 - end program\n? " );
       scanf( "%d", &menuChoice );
       return menuChoice;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can use fread and fwrite on text files, but they are designed for binary data so you can't be sure what you'll get if you use them with text data.

    OT - Is that deitel book any good? I considered picking it up the other day and was stopped dead by the $72 price tag. I bought The C++ Programming Language for less than that and it's the C++ bible.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Hi,

    I nearly read through half the book. The book teachs C, and a few chapters for JAVA and C++. I love its exercises. But quazh and you both said that the way the code was written in the deitel book is wrong? Then pls one of your two of some other programmers give a hand and really tell me whats wrong? I tried rb+ and wb, still the same error ( or bug ). thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 12:26 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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