How to remove all occurrences of a WORD from a FILE in C
Hello cboard,
I'm happy to have become a member. This thread is one of two to be posted now or latter. I am new to C so please excuse my non-technical speech. I have been learning by examples (inline, no calling), and I was doing a good job up until now. Since requiring something new for my project, all week long, from sun up to sun down I have not yet found or figured out *How to delete or remove a word from a file*. I read much C documentation to no avail (but great for other stuff). Google result are always *How to delete a word in a string* and they all require user input to the terminal which I don’t need. A picture is worth a thousand words. Could someone modify this example to do what I’m after. I modified this example 50 ways. Below I only include the unconnected FILE function instead of posting all the garbage I came up with while trying.
Any help would be greatly appreciated. Thank you.
Code:
/**
* 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];
/**
*char word[] = "remove me"; ............ something like this to search
*FILE *_read_0,*write_0;
*
* _read_0=fopen("1.txt","r"); ......... something like this to work
* write_0=fopen("2.txt","w");
*/
/* 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);
/**
* fclose(_read_0); ? ? ?
* fclose(write_0);
*/
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--;
}
}
}