Reputation: 27
how can i delete characters occurrences from a file in C
The program has to search in the file and then delete for example all "a" or all "b"
I found a program that removes words, but i don't know how to turn it to remove only characters
/**
* 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--;
}
}
}
And another one that i wrote :
#include<stdio.h>
#include<stdlib.h>
int SuppAppCara(char c, char nomfichier[50] ){
int i,j;
for (i=0; i<nomfichier ;i++)
{
if ( nomfichier[i]==c){
i--;
}
}
int main() {
FILE*f;
printf(" Saisir le nom du fichier:");
gets(nomfichier);
f=fopen(nomfichier,"r");
if (f==NULL){
printf("erreur d'ouverture");
exit(1);
}
FILE*f2;
f2=fopen("inter.txt","w");
printf(" entrer le caractére a supprimé : ");
scanf("%c",&c);
while( c=fgetc(f)!=EOF){
if (SuppAppCara(c,nomfichier[50]) == c){
fprintf(f2,"%s",nomfichier);
}
}
printf(" le caractere %c est supprimer avec succes ",c);
fclose(f2);
fclose (f);
remove(nomfichier);
rename("inter.txt",nomfichier);
}
}
The second program gives the following errors :
Please can you help me? Either with the first or second, i am not that strong in C language and i been working on it for few days
Thanks
Upvotes: 0
Views: 1097
Reputation: 1702
You can read the file character by character and only save those that don't match the character you want to remove.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c = 0;
const unsigned char discard = 'a';
const char *file_name = "input.txt";
FILE *input_file = NULL, *output_file = NULL;
if (!(input_file = fopen(file_name,"r")) || !(output_file = fopen("out.txt","w"))) /* Checking if the file pointers are NULL */
{
fprintf(stderr,"error handling"); /* Add error messages */
exit(EXIT_FAILURE);
}
while ((c = fgetc(input_file)) != EOF)
{
if (c != discard)
{
fputc(c,output_file);
}
}
fclose(input_file);
exit(EXIT_SUCCESS);
}
Upvotes: 3