Write a C Program Delete Line from text File using File Handling

Delete Line from text File


Write a C Program Delete Line from text File using File Handling. Here’s simple Program Delete Line from text File using File Handling in C Programming Language.


Below is the source code for C Program Delete Line from text File using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program Delete Line from text File using File Handling  */

#include <stdio.h>

int main()
{
    FILE *fileptr1, *fileptr2;
    char filename[40];
    char ch;
    int delete_line, temp = 1;

    printf("Enter file name: ");
    scanf("%s", filename);
    //open file in read mode
    fileptr1 = fopen(filename, "r");
    ch = getc(fileptr1);
    while (ch != EOF)
    {
        printf("%c", ch);
        ch = getc(fileptr1);
    }
    //rewind
    rewind(fileptr1);
    printf(" \n\n Enter line number of the line to be deleted:");
    scanf("%d", &delete_line);
    //open new file in write mode
    fileptr2 = fopen("C:\\Users\\acer\\Documents\\file5.txt", "w");
    ch = getc(fileptr1);
    while (ch != EOF)
    {
        ch = getc(fileptr1);
        if (ch == '\n')
            temp++;
            //except the line to be deleted
            if (temp != delete_line)
            {
                //copy all lines in file replica.c
                putc(ch, fileptr2);
            }
    }
    fclose(fileptr1);
    fclose(fileptr2);
    remove(filename);
    //rename the file replica.c to original name
    rename("C:\\Users\\acer\\Documents\\file5.txt", filename);
    printf("\n The contents of file after being modified are as follows:\n");
    fileptr1 = fopen(filename, "r");
    ch = getc(fileptr1);
    while (ch != EOF)
    {
        printf("%c", ch);
        ch = getc(fileptr1);
    }
    fclose(fileptr1);
    return 0;
}

OUTPUT : :


Enter file name: C:\\Users\\acer\\Documents\\file4.txt

 Write a C Program
Delete Line from text File
using File Handling.
Here's simple Program Delete Line
in C Programming Language.
Below is the source code for
C Program Delete Line from text File
using File Handling which is successfully
compiled and run on Windows System to
produce desired output as shown below :


 Enter line number of the line to be deleted:6

 The contents of file after being modified are as follows:

Write a C Program
Delete Line from text File
using File Handling.
Here's simple Program Delete Line
in C Programming Language.
C Program Delete Line from text File
using File Handling which is successfully
compiled and run on Windows System to
produce desired output as shown below :


Above is the source code for C Program Delete Line from text File using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply