C Program: Replace Specific Line in Text File
Text File Line Replacement in C: Step-by-Step Guide
A program to replace a specified line in a text file is a computer program that modifies the contents of a text file by replacing a particular line with new text. The program prompts the user to specify the line number they want to replace and the new text to replace it with. The program then reads the original file, locates the specified line, replaces it with the new text, and saves the modified content back to the same file. This type of program is useful when you want to update or edit a specific line in a large text file without having to manually search for and edit the line
Program 01: Replace Line in Text File: C Programming Example
#include<stdio.h>
int main(void) {
FILE *fp1, *fp2;
//'filename'is a 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);
fp1 = fopen(filename, "r");
//open file in read mode
c = getc(fp1);
//print the contents of file .
while (c != EOF) {
printf("%c", c);
c = getc(fp1);
}
//ask user for line number to be deleted.
printf(" \n Enter line number to be deleted and replaced");
scanf("%d", &del_line);
//take fp1 to start point.
rewind(fp1);
//open copy.c in write mode
fp2 = fopen("copy.c", "w");
c = getc(fp1);
while (c != EOF) {
if (c == '\n') {
temp++;
}
//till the line to be deleted comes,copy the content from one file to other
if (temp != del_line){
putc(c, fp2);
}
else //when the line to be deleted comes
{
while ((c = getc(fp1)) != '\n') {
}
//read and skip the line ask for new text
printf("Enter new text");
//flush the input stream
fflush(stdin);
putc('\n', fp2);
//put '\n' in new file
while ((c = getchar()) != '\n')
putc(c, fp2);
//take the data from user and place it in new file
fputs("\n", fp2);
temp++;
}
//continue this till EOF is encountered
c = getc(fp1);
}
//close both files
fclose(fp1);
fclose(fp2);
//remove original file
remove(filename);
//rename new file with old name opens the file in read mode
rename("copy.c", filename);
fp1 = fopen(filename, "r");
//reads the character from file
c = getc(fp1);
//until last character of file is encountered
while (c != EOF){
printf("%c", c);
//all characters are printed
c = getc(fp1);
}
//close the file pointer
fclose(fp1);
return 0;
}
Output:
Enter file name:abc.txt
hi.
hello
how are you?
hope the same
Enter line number of the line to be deleted and replaced:4
Enter new text: sayonara see you soon
hi.
hello
how are you?
sayonara see you soon
Explanation:
In this program, the user is asked to type the name of the file. The File by name entered by the user is opened in read mode.The line number of the line to be replaced is asked as input. Next, the data to be replaced is asked. A new file is opened in write mode named "copy. c". Now the contents of the original file are transferred into a new file and the line to be modified is deleted. New data is stored in its place and the remaining lines of the original file are also transferred. The copied file with modified contents is replaced with the original file's name. Both the file pointers are closed and the original file is again
Example 02: C program that replaces a specified line in a text file
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LINE_LENGTH 1000
int main() {
char filename[50], temp_filename[50], line[MAX_LINE_LENGTH];
int line_num, curr_line_num = 1;
FILE *file, *temp_file;
printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the line number to replace: ");
scanf("%d", &line_num);
// Open the original file for reading
file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.");
return 1;
}
// Create a temporary file for writing
sprintf(temp_filename, "%s.temp", filename);
temp_file = fopen(temp_filename, "w");
if (temp_file == NULL) {
printf("Error creating temporary file.");
fclose(file);
return 1;
}
// Read lines from the original file and write them to the temporary file
while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
if (curr_line_num == line_num) {
// Replace the line with a new line
printf("Enter the new line: ");
fgets(line, MAX_LINE_LENGTH, stdin);
fputs(line, temp_file);
} else {
// Write the original line to the temporary file
fputs(line, temp_file);
}
curr_line_num++;
}
// Close the original and temporary files
fclose(file);
fclose(temp_file);
// Replace the original file with the temporary file
remove(filename);
rename(temp_filename, filename);
printf("Line %d has been replaced.\n", line_num);
return 0;
}
Explanation: In this program, the user is prompted to enter the filename and the line number to replace. The program then opens the original file for reading and creates a temporary file for writing. It reads each line from the original file and writes it to the temporary file, except for the specified line number, which is replaced with a new line entered by the user. Finally, the original file is replaced with the temporary file.
opened in read mode and the contents of the original file is printed as output.