Sunday, 24 August 2014

Filled Under:

C Program Delete a specific Line from a Text File

Share
This C Program delete a specific line from a text file.Here is source code of the C Program to delete a specific line from a text file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
C Program Delete a specific Line from a Text File



C Program Delete a specific Line from a Text File


 
#include <stdio.h>
 
int main()
{
    FILE *fileptr1, *fileptr2;
char myfile[50];
char ch;
int delete_line, temp = 1;
 
printf("Enter file name: ");
scanf("%s", myfile);
//open file in read mode
    fileptr1 = fopen(myfile, "r");
    ch = getc(fileptr1);
 ` while (ch != EOF)
{
printf("%c", ch);
        ch = getc(fileptr1);
}
//rewind
rewind(fileptr1);
printf(" \n Enter line number of the line to be deleted:");
scanf("%d", &delete_line);
//open new file in write mode
    fileptr2 = fopen("replica.c", "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(myfile);
//rename the file replica.c to original name
rename("replica.c", myfile);
printf("\n The contents of file after being modified are as follows:\n");
    fileptr1 = fopen(myfile, "r");
    ch = getc(fileptr1);
while (ch != EOF)
{
printf("%c", ch);
        ch = getc(fileptr1);
}
fclose(fileptr1);
return 0;
}