Delete Line from Text File: C Program with Detailed Explanation
C Program: Deleting a Specific Line from a File
In this program, the user is asked for a filename he needs to change. The user is also asked for the line number that is to be deleted. The filename is stored in 'filename'. The file is opened and all the data is transferred to another file except that one line the user specifies to delete.
Step-by-Step Explanation of Deleting Lines in C
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
//consider 40 character string to store filename
char filename[40];
char c;
int del_line, temp = 1;
//asks user for file name
printf("Enter file name: ");
//receives file name from user and stores in 'filename'
scanf("%s", filename);
//open file in read mode
fp1 = fopen(filename, "r");
c = getc(fp1);
//until the last character of file is obtained
while (c != EOF)
{
printf("%c", c);
//print current character and read next character
c = getc(fp1);
}
//rewind
rewind(fp1);
printf(" \n Enter line number of the line to be deleted:");
//accept number from user.
scanf("%d", &del_line);
//open new file in write mode
fp2 = fopen("copy.c", "w");
c = getc(fp1);
while (c != EOF) {
c = getc(fp1);
if (c == '\n')
temp++;
//except the line to be deleted
if (temp != del_line)
{
//copy all lines in file copy.c
putc(c, fp2);
}
}
//close both the files.
fclose(fp1);
fclose(fp2);
//remove original file
remove(filename);
//rename the file copy.c to original name
rename("copy.c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fp1 = fopen(filename, "r");
c = getc(fp1);
while (c != EOF) {
printf("%c", c);
c = getc(fp1);
}
fclose(fp1);
return 0;
}
Output:
Enter file name:abc.txt
hi.
hello
how are you?
I am fine
hope the same
Enter line number of the line to be deleted:4
The contents of file after being modified are as follows:
hi.
hello
how are you?
hope the same
Attention please: In this program, the user is asked for a filename that needs to be modified. The entered file name is stored in a char array 'filename'. This file is opened in read mode using file pointer 'fp1'. Character 'c' is used to read characters from the file and print them to the output. The user is asked for the line number in the file to be deleted. The file pointer is rewinded back and all the lines of the file except for the line to be deleted are copied into another file "copy. c". Now "copy. c" is renamed to the original filename. The original file is opened in read mode and the modified contents of the file are displayed on the screen.
C Program Logic: How to Remove a Chosen Line
#include<stdio.h>
#include<string.h>
#define BUFFER_SIZE 1024
int main() {
char file_name[50], line_to_delete[BUFFER_SIZE];
int line_number = 0, temp = 1;
printf("Enter the name of the file: ");
fgets(file_name, 50, stdin);
file_name[strcspn(file_name, "\n")] = 0;
printf("Enter the line number to be deleted: ");
scanf("%d", &line_number);
FILE *file_ptr, *temp_ptr;
file_ptr = fopen(file_name, "r");
temp_ptr = fopen("temp.txt", "w");
while (fgets(line_to_delete, BUFFER_SIZE, file_ptr) != NULL) {
if (temp != line_number) {
fputs(line_to_delete, temp_ptr);
}
temp++;
}
fclose(file_ptr);
fclose(temp_ptr);
remove(file_name);
rename("temp.txt", file_name);
printf("Successfully deleted the %dth line from the file '%s'.\n", line_number, file_name);
return 0;
}
In this program, we first ask the user to enter the name of the file and the line number to be deleted. We then open the file in read mode and a temporary file in write mode. We read the file line by line and write it to the temporary file if it's not the line we want to delete. Once we have read the entire file, we close both files and delete the original file. We then rename the temporary file to the original file name.
Note that this program assumes that the input file contains the text. If the file contains binary data, it may not work as expected. Also, this program does not handle errors such as file not found, invalid line numbers, or file permissions issues. You may want to add additional error handling code to make it more robust