How do I delete a specific line from text file in C?

Viewed 77173

Example:

George 50 40
Lime 30 20
Karen 10 60

do {
    printf("\nInput player name:[1..10] ");
    fgets(name,10,stdin);
}

Input name: Lime

Then the text file will be:

George 50 40
Karen 10 60

4 Answers

Consider a file like an array. If we want to delete an element at index i of an array, we shift all elements from i+1 one step towards left and then logically reduce the size of array.

I am applying same princliple on files. In the end ftruncate() is used to reduce the size of file and hence removing the end parts of the file.This code works fine for large files too, as at any time only one byte is being stored in memory.

#include<errno.h>
#include<unistd.h>
#include<stdio.h>
/*
 * Description: fdelete() deletes 'bytes' bytes of data from the stream pointed to by fp. 
 *              bytes will be deleted from the CURRENT FILE POSITION.
 *              File must be opened in read + write mode while passing file pointer 
 *              to this function.
 *              File position before calling the function and after the 
 *              function returns will be the same.
 * Return Values: returns 0 on success and errno on failure. Kindly use perror("") 
 *                to print error if non-0 return value returned.
 */
int fdelete(FILE* fp, int bytes) {
    
    // to store each byte/char from file
    char byte;
    long readPos = ftell(fp) + bytes, writePos = ftell(fp), startingPos = writePos;
    // start reading from the position which comes after the bytes to be deleted
    fseek(fp, readPos, SEEK_SET);
    while (fread(&byte, sizeof(byte), 1, fp)) {
        // modify readPos as we have read right now
        readPos = ftell(fp);
        // set file position to writePos as we are going to write now
        fseek(fp, writePos, SEEK_SET);
        
        // if file doesn't have write permission
        if (fwrite(&byte, sizeof(byte), 1, fp) == 0) 
            return errno;
        // modify writePos as we have written right now
        writePos = ftell(fp);
        // set file position for reading
        fseek(fp, readPos, SEEK_SET);
    }

    // truncate file size to remove the unnecessary ending bytes
    ftruncate(fileno(fp), writePos);
    // reset file position to the same position that we got when function was called.
    fseek(fp, startingPos, SEEK_SET); 
    return 0;
}

You don't need to create a new file. You can open the original file with r+ and store its contents into an array (following these steps). Then you can use a for loop to scan the array for the line you want to skip, and delete that line. Then you can overwrite the contents of the file using fseek(filename,0,SEEK_SET) (to reset the position indicator of the file) and using a for loop and fprintf to copy the contents from the modified array to the file. (However, with this method you will need to enter an extra blank line in the last for loop to overwrite the last line of the original file.)

Related