4

I know that the function fseek() can be used to output data to a specific location in a file. But I was wondering if I use fseek() to move to the middle of the file and then output data. Would the new data overwrite the old data? For example if I had a file containing 123456789 and I used fseek() to output newdata after the 5 would the file contain 12345newdata6789 or would it contain 12345newdata.

3 Answers 3

7

Writing data in the "middle" of a file will overwrite existing data. So you would have '12345newdata'.


EDIT: As mentioned in the comments below, it should be noted that this overwrites data without truncating the rest of the file. As an extended version of your example, if you wrote newdata after the 5 in a file containing 1234567890ABCDEFG, you would then have 12345newdataCDEFG, not 12345newdata.

2
  • 1
    It doesn't truncate the entire file. But it overwrites data already present at the positions being written to. Nov 18, 2012 at 0:03
  • 1
    Ah, true. There wasn't enough data in the file to see the difference. Sorry. I think it would be worth mentioning in the answer, though.
    – rici
    Nov 18, 2012 at 0:05
2

Yes it lets you do that, and those files are called "Random Access Files". Imagine you have already a set file ( with the structure but empty ), in that case you can fill the "slots" you want, or in the case the slot is filled with data you can overwrite on it.

typedef struct{
    int number;
    char name[ 20 ];
    char lastname[ 20 ];
    float score;
}students_t;

/* Supposing that you formatted the file already and the file is opened. */
/* Imagine the students are listed each one has a record. */

void modifyScore( FILE * fPtr ){
    students_t student = { 0, "", "", 0.0 };
    int nrecord;
    float nscore;

    printf( "Enter the number of the student:" );
    scanf( "%d", &record )
    printf( "Enter the new Score:" );
    scanf( "%f", &nscore ); // this is a seek example so I will not complicate things.

    /*Seek the file ( record - 1 ), because the file starts in position 0 but the list starts in 1*/
    fseek( fPtr, ( record  - 1  ) * sizeof ( students_t ), SEEK_SET );

    /* Now you can read and copy the slot */
    fread( fPtr, "%d%s%s%f", &student.number, student.name, student.lastname, &student.score );

    /* Seek again cause the pointer moved. */
    fseek( fPtr, ( record  - 1  ) * sizeof ( students_t ), SEEK_SET );
    student.score = nscore;

    /*Overwrite his information, only the score will be altered. */
    fwrite( &student, sizeof( student_t ), 1, fPtr );
}

This is how it works (picture obtained from Deitel-How to program in C 6th Edition):

Deitel-How to program in C 6th Edition

6
  • 1
    @Bonsanto would I have to load the entire file into memory in order for your method to work? The goal I am trying to accomplish is extract portions of the file into memory(not the entire file), edit them, and then "save" the data to the file without replacing any data that was not extracted. Nov 18, 2012 at 0:10
  • 1
    You just need a formatted file, copy the slot you want to edit using a bypass in my example (student variable), modify the member you want and after that seek again and then overwrite on that slot Nov 18, 2012 at 0:13
  • @Bonstanto first off thank you very much for your help so far. But i am new to C and a little bit confused. From what I understand you seek threw the file until you get to the part you want to edit then extract it into memory as a struct edit it and then output it back to the same spot in the file. Am I close? Also will this method still work if the new data exceeds the byte size of the old data? Nov 18, 2012 at 0:42
  • You are very close, but if you use Random Access Files, You have to be sure that your structure is enough robust to hold the maximum permitted value. Here is a related post: stackoverflow.com/questions/13438941/… Nov 18, 2012 at 13:55
  • 1
    @Mawg I extracted it from Learn to Program in C by Deitel. BTW, should add credits Sep 13, 2016 at 13:20
2

You probably know this but fseek() merely moves the associated position indicator and doesn't dictate per se whether the proceeding output function will overwrite or insert.

You're probably using fwrite() or some other plain vanilla output function, and these will overwrite, giving you "12345newdata" instead of the inserted variant.

On the other hand, you could roll your own inserting function (I don't think there's a stock stdio.h function for this), and call this after fseek() to get your desired insertion.

Something like this could suffice:

insert(const void *ptr, size_t len, FILE *fp) {
    char tmp[len];
    size_t tmplen;

    while (len) {
        // save before overwriting
        tmplen = fread(tmp, 1, sizeof(tmp), fp);
        fseek(fp, -tmplen, SEEK_CUR);

        // overwrite
        fwrite(ptr, len, 1, fp);

        // reloop to output saved data
        ptr = tmp;
        len = tmplen;
    }
}

(Error handling on fread() and fwrite() left out for verbosity.)

2
  • Antak thank you for response and this solution would work but I want to insert the new data without having to buffer the old data that would be overwritten. I know this seems like an unnecessary constraint but the problem I am working on requires the editing of large files. Which would be cumbersome to hold in memory. Nov 18, 2012 at 19:30
  • You don't need to buffer the entire file. FWIW, the above example only uses the same amount buffer as the data you're putting out (and no way dependent on the size of the actual file).
    – antak
    Nov 19, 2012 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.