In this tutorial you will learn about the C Program to Replace a Specific Line in a Text File and its application with practical example.
In this tutorial, we will learn to create c program to replace a specific line in a text file. The file must exist in the directory where the executable file of this program is present.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics: C Pointers, C File Handling, C Strings and C Input Output.
Create required files
Let’s first create files (inside the current project directory) as following:
- f1.txt – file to count line
The file must be saved in the folder where you are saving program file. Put the following content in the file:
f1.txt
1 2 3 4 5 |
I'm on line 1 I'm on line 2 I'm on line 3 I'm on line 4 I'm on line 5 |
How to Replace a Specific Line in a Text File?
The source file will be opened and being read through character by character and store its contents in temporary file. When we reached the specific line we will copy new line content to temporary file. Finally, the original source file will be deleted and the temporary file will renamed to source file name.
C Program to Replace a Specific Line in a Text File
In this C program we will replace a specific line in a text file. The source file will be opened in “read” mode and a temporary target file is opened in “write” mode. The source file (f1.txt) will be opened and being read through character by character and store its contents in temporary file. When we reached the specific line we will copy new line content to temporary file. The file being read copied until its EOF (end of file) is reached. Finally, the original source file will be deleted and the temporary file will renamed to source file name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#include <stdio.h> #include <stdlib.h> #define BUFFER_SIZE 1000 int main() { /* File pointer to hold reference of input file */ FILE * fPtr; FILE * fTemp; char path[100]; char buffer[BUFFER_SIZE]; char newline[BUFFER_SIZE]; int line, count; clrscr(); printf("Enter source file name: "); scanf("%s", path); printf("Enter line number to be replaced: "); scanf("%d", &line); /* Remove extra new line character from stdin */ fflush(stdin); printf("Replace '%d' line with: ", line); fgets(newline, BUFFER_SIZE, stdin); /* Open all required files */ fPtr = fopen(path, "r"); fTemp = fopen("replace.tmp", "w"); /* fopen() return NULL if unable to open file in given mode. */ if (fPtr == NULL || fTemp == NULL) { /* Unable to open file hence exit */ printf("\nUnable to open file.\n"); printf("Please check whether file exists and you have read/write privilege.\n"); exit(EXIT_SUCCESS); } /* * Read line from source file and write to destination */ count = 0; while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL) { count++; /* If current line is line to replace */ if (count == line) fputs(newline, fTemp); else fputs(buffer, fTemp); } /* Close all files to release resource */ fclose(fPtr); fclose(fTemp); /* Delete original source file */ remove(path); /* Rename temporary file as original file */ rename("replace.tmp", path); printf("\nSuccessfully replaced '%d' line with '%s'.", line, newline); return 0; } |
Output:-
f1.txt :- After replacing line 2
1 2 3 4 5 |
I'm on line 1 I am changed I'm on line 3 I'm on line 4 I'm on line 5 |
Open source file in read mode, store its reference to fPtr
.Create and open a temporary file with name replace.tmp
, store its reference to fTemp
.Input line number to replace in file from user. Store it in some variable say line
.Input new line from user to replace with, store it in newline
.Initialize a count
variable with 0.Read a line from file and store it in buffer
. Increment count
by 1.If count == line
, then current line should be replaced with newline
. Means if (count == 0)
then write newline
to fTemp
, otherwise write buffer
to fTemp
. Repeat step 6-8 till end of file.Finally close all files.Delete the original source file and rename temporary fTemp
file path as of source file.