Thread: need help with updating a file

  1. #1
    Registered User mc088's Avatar
    Join Date
    Jan 2017
    Posts
    19

    need help with updating a file

    Hi,

    Just trying to write some code so I can figure out how to allow user to add entries to a file or modify (update) an existing entry in a file.

    Add functionality will come later, but for now, I am working on the updating functionality.


    That said, in the code I wrote below, I would like to know what I am doing wrong in the updateRecord function (I think the way that I'm employing should work wherein you copy the contents of first file to second file all the way up to the line that needs modifying, then ask user for the replacement stuff, and then write it to the second file and finish copying rest of first file if anything more)

    At least, that's the way I'm currently thinking of handling the updating. Seems that program so far can print out the replacement information but perhaps at wrong location / formatting is off / or a initialized 0 value that should be replaced isn't being removed.

    Been trying for some time to get this to work, so just wondered if somebody kindly had any suggestions to get me on the right track. (would like to know / learn where I'm going wrong in my code)

    Finally, is doing file modifications by way of using two files (the original file and a temp file that will be renamed) a good way or is there a better way than this? I have seen other people's code that does what I would like / similar action, but they involved using fseek/fread (which I have not looked into yet).

    Thanks!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define DIR "D:/Documents/Visual Studio 2015/Projects/C/
    REC_MIN 1
    REC_MAX 5
     
    struct itemList
    {
           int recordNum;
           char item[20];
           int quantity;
           double cost;
    };
     
    int main()
    {
         initializeFile();
         updateRecord();
         return 0;
    }; 
     
    /**************************************************************************************/
     
    void initializeFile()
    {
           FILE *ifPtr = fopen(DIR "file1.dat", "w");
           int i;
          
           struct itemList ItemList = { 0, "", 0, 0.0 };
     
           if (ifPtr  == NULL)
           {
                  printf("Cannot open file1.dat file.\n");
           }
           else
           {
     
                  fprintf(ifPtr, "%6s%16s%15s%10s\n", "Record #", "Item", "Quantity", "Cost");
                  fprintf(ifPtr, "%6s%16s%15s%10s\n", "========", "====", "========", "====");
     
                  for (i = REC_MIN; i <= REC_MAX; i++)
                  {
                 
                         fprintf(ifPtr, "%6d%16s%13d%14.2f\n", ItemList.recordNum, ItemList.item, ItemList.quantity, ItemList.cost);
                  }
           }
    
           fclose(ifPtr);
    }
     
    /**************************************************************************************/
     
     
    void updateRecord()
    {
           FILE *ofPtr = fopen(DIR "file1.dat", "r");
           FILE *nfPtr = fopen(DIR "file2.dat", "w");
           char c;
           int recordToUpdate;
           int lineCount = 1; 
           struct itemList ItemList = { 0, "", 0, 0.0 };
     
           printf("\nEnter record to be updated (%d - %d)\n", REC_MIN, REC_MAX);
     
           do
           {
                  printf("? ");
                  scanf("%d", &recordToUpdate);
           } while (recordToUpdate < REC_MIN || recordToUpdate > REC_MAX);
     
           recordToUpdate += 3; // offset the user input to accomodate the lines used by header
     
           while ((c = fgetc(ofPtr)) != EOF)     // read character from file until EOF
           {
                  // get the line count (print everyting before and after the line to be deleted)
                  if (c == '\n')
                  {
                         lineCount++;
                  }
     
                  // output all record, except for the line that is to be updated
                  // when the line to be updated is reached, then ask for the update, and use that
                  // instead
                  if (lineCount != recordToUpdate)
                  {
                         putc(c, nfPtr);
                  }
                  else
                  {
                         printf("\nEnter new item name, quantity, cost for record #%d\n", recordToUpdate -= 2);
                         scanf("%s%d%lf", &ItemList.item, &ItemList.quantity, &ItemList.cost);
                         fprintf(nfPtr, "   \n%d\t\t\t   %s\t   %d\t\t%0.2f\n", ItemList.recordNum, ItemList.item, ItemList.quantity, ItemList.cost);
                  }
           }
     
           fclose(nfPtr);
           fclose(ofPtr);
     
           remove(DIR "file1.dat");
           rename(DIR "fil2.dat", DIR "file1.dat");
           printf("Successfully updated record %d.\n\n", recordToUpdate);
    }
    Last edited by mc088; 02-17-2017 at 04:08 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    > fprintf(ifPtr, "%6d%16s%13d%14.2f\n", ItemList.recordNum, ItemList.item, ItemList.quantity, ItemList.cost);
    You print this when creating the record.

    > fprintf(nfPtr, " \n%d\t\t\t %s\t %d\t\t%0.2f\n", ItemList.recordNum, ItemList.item, ItemList.quantity, ItemList.cost);
    You print this when updating the record.

    The first step is to be consistent.

    > while ((c = fgetc(ofPtr)) != EOF)
    To be honest, you're dealing with text files.
    So use fgets() to fetch a whole line at once then decide whether to
    - copy it to the output file as is
    - replace it with something else
    - or not write anything at all.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-25-2011, 02:59 AM
  2. Reading and then updating a file
    By kopite in forum C Programming
    Replies: 3
    Last Post: 12-02-2003, 04:15 PM
  3. Updating file name
    By gazsux in forum C Programming
    Replies: 2
    Last Post: 04-24-2003, 03:11 PM
  4. updating a file
    By toad in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:39 PM
  5. Updating a file
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-29-2002, 03:03 AM

Tags for this Thread