Thread: updating a file

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    updating a file

    I am trying to update a record in a text file. What happens is the program reads the file into an array it then sorts the array. The user is then asked to enter a item code for the item that they want to update. The program then searches the sorted array if the item code is found then enter new details. The program then writes the new data to disk but it is written at the EOF. here is the code i am using. is it poosible to update a record using this code. I think the fseek is the problem line but i cant work out how to get to the fseek to the current record.

    {
    struct stock new_s;
    int s, size = 0, i;
    char search[14];
    char (*code)[14] = malloc(size * sizeof(char[14]));
    FILE *fp;

    s = sizeof(struct stock);

    if((fp = fopen("stock.txt", "a+")) == NULL)
    {
    err_msg("*** Read error -- ensure file exists ***");
    }

    do
    {
    if(fread(&new_s, sizeof(struct stock), 1, fp) != s)
    {
    if(feof(fp))
    {
    break;
    }
    strcpy(code[size], new_s.itemCode);
    size++;
    }
    }while(!feof(fp));
    sortArrayS(code, size);
    printf("\n%d", size);

    fflush(stdin);
    printf("\n\n\n\n\n");
    US: getItemCode(new_s.itemCode);
    strcpy(search, new_s.itemCode);

    if(searchArrayS(code, size, search) >= 0)
    {
    get_new_code(&new_s);
    fseek(fp, (long)-s, 1);
    fwrite(&new_s,s,1,fp);
    }
    else
    {
    printf("\nThe item you were looking for is not on the file please try again");
    goto US;
    }
    fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Because this error isn't in your code, I find it highly unlikely that this isn't just a problem with your compiler, but I know that there are different file settings you can have where it overwrites, continues the file, clears and then writes, etc.. But you didn't specify a mode (at least no where that I could find) so it really should've defaulted to clear and write. I know this doesn't solve the problem but it's all I could think of and is at least food for thought. Good luck.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well apart from the lack of tags, the main problem seems to be that size==0 when the malloc is called.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    Well apart from the lack of tags, the main problem seems to be that size==0 when the malloc is called.
    And ...
    >goto <- yuck!

    >fflush(stdin); <- yuck

    >if (fread(&new_s, sizeof(struct stock), 1, fp) != s)
    Incorrect comparison! If the read is successful, the return code is 1.

    >} while (!feof(fp));
    Don't control the loop like this, use the rc from fread.

    >char(*code)[14] = malloc(size * sizeof(char[14]));
    >strcpy(code[size], new_s.itemCode);
    What are you upto here?

    >fseek(fp, (long) -s, 1);
    try and use the #defines that came with compiler, it makes reading easier (SEEK_CUR)

    >if ((fp = fopen("stock.txt", "a+")) == NULL)
    >{
    > err_msg("*** Read error -- ensure file exists ***");
    Mode a+ will create the file if it doesn't exist. Also, the internal stream is set to the end of the file, so subsequent reads won't work.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>I am trying to update a record in a text file.

    No guarantee the record to be replaced is the same size on the file as the old one. Use binary or rewrite the whole file. If you are sorting it then why not rewrite the whole file?

    Or start from begining of file, read a record, compare the the ones flaged for change, write as needed.

    >>What happens is the program reads the file into an array it then sorts the array.

    Are you searching the unsorted array (or file) for the correct record number to replace or the sorted array? (where the records may have changed order)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM