Write a C program to remove a given word from a text file. Logic to remove word from file in C programming. How to remove word from file in C program.
C programming support overwrite, insert and append operations on file content. It does not support delete operation. You cannot delete file contents directly. In this post I will explain you how to delete a word from file in C.
Required knowledge
Basic Input Output, String, File Handling
Logic to remove word from file
Step by step descriptive logic to remove a word from text file.
- Open source file in
r
(read) mode. Store its reference in aFILE
pointer variable sayfPtr
. - Create and open a temporary file say
delete.tmp
inw
(write) mode. Store its reference in a variable sayfTemp
. - Read word to remove from user in some variable say
toRemove
. - Read a line from source file
fPtr
and store it in temporarybuffer
variable. - Remove all occurrence of given word from
buffer
and writebuffer
to temporary filefTemp
. - Repeat step 4-5 till end of source file.
- Close both file
fPtr
andfTemp
. - Delete source file using
remove()
function. - Rename temporary file with source file name using
rename()
function. And we are done.
Program to remove word from file
/**
* C program to delete a word from file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
void removeAll(char * str, const char * toRemove);
int main()
{
FILE * fPtr;
FILE * fTemp;
char path[100];
char toRemove[100];
char buffer[1000];
/* Input source file path path */
printf("Enter path of source file: ");
scanf("%s", path);
printf("Enter word to remove: ");
scanf("%s", toRemove);
/* Open files */
fPtr = fopen(path, "r");
fTemp = fopen("delete.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
* file after removing given word.
*/
while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
{
// Remove all occurrence of word from current line
removeAll(buffer, toRemove);
// Write to temp file
fputs(buffer, fTemp);
}
/* Close all files to release resource */
fclose(fPtr);
fclose(fTemp);
/* Delete original source file */
remove(path);
/* Rename temp file as original file */
rename("delete.tmp", path);
printf("\nAll occurrence of '%s' removed successfully.", toRemove);
return 0;
}
/**
* Remove all occurrences of a given word in string.
*/
void removeAll(char * str, const char * toRemove)
{
int i, j, stringLen, toRemoveLen;
int found;
stringLen = strlen(str); // Length of string
toRemoveLen = strlen(toRemove); // Length of word to remove
for(i=0; i <= stringLen - toRemoveLen; i++)
{
/* Match word with string */
found = 1;
for(j=0; j < toRemoveLen; j++)
{
if(str[i + j] != toRemove[j])
{
found = 0;
break;
}
}
/* If it is not a word */
if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0')
{
found = 0;
}
/*
* If word is found then shift all characters to left
* and decrement the string length
*/
if(found == 1)
{
for(j=i; j <= stringLen - toRemoveLen; j++)
{
str[j] = str[j + toRemoveLen];
}
stringLen = stringLen - toRemoveLen;
// We will match next occurrence of word from current index.
i--;
}
}
}
Suppose contents of <strong>data/file4.txt</strong>
I love programming.
Programming with files is fun.
Learning C programming at Codeforwin is simple and easy.
Output
Enter path of source file: data\file4.txt Enter word to remove: is All occurrence of 'is' removed successfully.
After removing contents of <strong>data/file4.txt</strong>
I love programming.
Programming with files fun.
Learning C programming at Codeforwin simple and easy.
Also read
Happy coding 😉