简体   繁体   English

从文本文件中删除出现的字符

[英]Remove Character Occurences from Text File

how can i delete characters occurrences from a file in C如何从 C 中的文件中删除出现的字符

The program has to search in the file and then delete for example all "a" or all "b"程序必须在文件中搜索然后删除所有“a”或所有“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无论是第一个还是第二个,我在 C 语言方面都不是很强,我已经研究了几天

Thanks谢谢

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM