Search code examples
cseekfseek

C Program: How to properly use lseek() or fseek() to modify a certain part of a file?


I have a binary file in CSV format that contains multiple records. Each record is entered in as user_name, last_name, first_name, num_wins, num_losses, num_ties. I am creating a method to update a certain players record of wins,losses and ties. The user inputs the username to change followed by the changes to the wins, losses and ties. For example smithj 4 5 1. This will find the smithj record and add 4 to the wins etc.

How do I use lseek or fseek to accomplish this? Or is there another way to do this?


Solution

  • Response to request for write-up of example - assuming fixed size records:

    enum { RECSIZE = 256 };
    
    char buffer[RECSIZE];
    FILE *fp = ...;   // File to read from and write to
    
    int recnum = 37;  // Counting from record 0
    long offset = recnum * RECSIZE;
    
    if (fseek(fp, offset, SEEK_SET) != 0)
        ...error...
    if (fread(buffer, sizeof(buffer), 1, fp) != 1)
        ...error...
    ...modify buffer...
    if (fseek(fp, offset, SEEK_SET) != 0)
        ...error...
    if (fwrite(buffer, sizeof(buffer), 1, fp) != 1)
        ...error...
    

    Repeat the code from the declaration of recnum to the end once for each record that must be modified (think loop).

    With variable size records that change size as you edit them, you have to work a lot harder - so much so that it is probably simpler to copy the old file into memory one record at a time, modifying the record appropriately, and then writing the modified record to a new file.