Write a C program to remove a given line from a file. How to remove a given line from a text file in C programming. Logic to remove specific line from a file in C program.
In previous post I explained to remove a word from file. In this post we will continue further and will learn to remove a given line from file. Logic to remove word or line from a file are alike. So, lets get started and code it.
Required knowledge
Basic Input Output, Pointers, Strings, Functions, File Handling
Logic to remove specific line from a file
Step by step descriptive logic to remove a given line from file.
- Input file path and line number to remove from user. Store it in some variable say
path
andline
. - Open file in
r
(read) mode, store its reference insrcFile
. - Create and open a temporary file in
w
(write) mode. Store its reference intempFile
. - Initialize a
count = 1
variable to keep track of current line number of source file. - Read a line from source file
srcFile
. Store it in some variable saybuffer
. - If current line number is not equal to line to remove i.e.
if(line != count)
then writebuffer
totempFile
. - Increment
count++
. - Repeat step 5-7 till end of source file.
- Close both files i.e.
srcFile
andtempFile
. - Delete our original source file
srcFile
. - Rename temporary file with source file path.
I have used functions for this program, since it provides modularity. I have created two functions deleteLine()
and printFile()
to delete a given line and print a given file on console.
Program to remove specific line from a file
/**
* C program to delete specific line from a file.
*/
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
/* Function declarations */
void deleteLine(FILE *srcFile, FILE *tempFile, const int line);
void printFile(FILE *fptr);
int main()
{
FILE *srcFile;
FILE *tempFile;
char path[100];
int line;
/* Input file path and line number */
printf("Enter file path: ");
scanf("%s", path);
printf("Enter line number to remove: ");
scanf("%d", &line);
/* Try to open file */
srcFile = fopen(path, "r");
tempFile = fopen("delete-line.tmp", "w");
/* Exit if file not opened successfully */
if (srcFile == NULL || tempFile == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write previleges.\n");
exit(EXIT_FAILURE);
}
printf("\nFile contents before removing line.\n\n");
printFile(srcFile);
// Move src file pointer to beginning
rewind(srcFile);
// Delete given line from file.
deleteLine(srcFile, tempFile, line);
/* Close all open files */
fclose(srcFile);
fclose(tempFile);
/* Delete src file and rename temp file as src */
remove(path);
rename("delete-line.tmp", path);
printf("\n\n\nFile contents after removing %d line.\n\n", line);
// Open source file and print its contents
srcFile = fopen(path, "r");
printFile(srcFile);
fclose(srcFile);
return 0;
}
/**
* Print contents of a file.
*/
void printFile(FILE *fptr)
{
char ch;
while((ch = fgetc(fptr)) != EOF)
putchar(ch);
}
/**
* Function to delete a given line from file.
*/
void deleteLine(FILE *srcFile, FILE *tempFile, const int line)
{
char buffer[BUFFER_SIZE];
int count = 1;
while ((fgets(buffer, BUFFER_SIZE, srcFile)) != NULL)
{
/* If current line is not the line user wanted to remove */
if (line != count)
fputs(buffer, tempFile);
count++;
}
}
Output
Enter file path: data\file3.txt Enter line number to remove: 2 File contents before removing line. I love programming. I am learning C programming at Codeforwin. Programming with files is fun. Learning C programming at Codeforwin is simple and easy. File contents after removing 2 line. I love programming. Programming with files is fun. Learning C programming at Codeforwin is simple and easy.
Happy coding 😉