Search code examples
cinsertfseek

Can fseek() be used to insert data into the middle of a file? - C


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.


Solution

  • 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