一次尝试使用C程序将文件中的所有数字(而非特定数字)替换为星号吗?

时间:2018-11-05 11:50:34

标签: c

我如何在包含数字和单词的文件中将所有数字都更改为星号?我的代码只能一次更改一次,而且很忙。在此代码中,我已经插入了打开文件,替换并关闭文件。供您参考,这是我作为一名大学生的第一年项目,我还不擅长使用c语言进行编程。因此,我一直在抓挠头,寻找可能的一切帮助,但无济于事。年项目是数字编辑,但总体而言,这意味着将包含数字(零至九)(在文件中)的所有敏感信息替换为星号。敏感信息包括信用卡号,借记卡号,银行帐号,住所地址,甚至电话数字。我衷心感谢那些希望帮助我的人!谢谢!

#include <stdio.h>
  #include <string.h>
  #define MAX 256

  int main() {
        FILE *fp1, *fp2;
        char word[MAX], fname[MAX];
        char string[MAX], replace[MAX];
        char temp[] = "temp.txt", *ptr1, *ptr2;

        /* get the input file from the user */
        printf("Enter your input file name:");
        fgets(fname, MAX, stdin);
        fname[strlen(fname) - 1] = '\0';

        /* get the word to delete from the user */
        printf("Enter the word to be replaced:");
        scanf("%s", word);

        /* get the word to replace */
        printf("Enter your replace word:");
        scanf("%s", replace);

        /* open input file in read mode */
        fp1 = fopen(fname, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open the input file!!\n");
                return 0;
        }

        /* open temporary file in write mode */
        fp2 = fopen(temp, "w");

        /* error handling */
        if (!fp2) {
                printf("Unable to open temporary file!!\n");
                return 0;
        }

        /* delete the given word from the file */
        while (!feof(fp1)) {
                strcpy(string, "\0");
                /* read line by line from the input file */
                fgets(string, MAX, fp1);

                /*
                 * check whether the word to delete is
                 * present in the current scanned line
                 */
                if (strstr(string, word)) {
                        ptr2 = string;
                        while (ptr1 = strstr(ptr2, word)) {
                                /*
                                 * letters present before
                                 * before the word to be replaced
                                 */
                                while (ptr2 != ptr1) {
                                        fputc(*ptr2, fp2);
                                        ptr2++;
                                }
                                /* skip the word to be replaced */
                                ptr1 = ptr1 + strlen(word);
                                fprintf(fp2, "%s", replace);
                                ptr2 = ptr1;
                        }

                        /* characters present after the word to be replaced */
                        while (*ptr2 != '\0') {
                                fputc(*ptr2, fp2);
                                ptr2++;
                        }
                } else {
                        /*
                         * current scanned line doesn't 
                         * have the word that need to be replaced
                         */
                        fputs(string, fp2);
                }
        }

        /* close the opened files */
        fclose(fp1);
        fclose(fp2);

        /* remove the input file */
        remove(fname);
        /* rename temporary file name to input file name */
        rename(temp, fname);
        return 0;
  }

1 个答案:

答案 0 :(得分:0)

如果要替换字符,请不要编写代码替换单词的程序,这会给程序带来不必要的复杂性,从而使工作变得很困难。下面是一个非常简单的方法。

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define MAX 256

bool isNum(char c)
{
    if (c >= '0' && c <= '9')
        return true;
    else
        return false;
}

int main(void) {
    FILE *fp1, *fp2;
    char fname[MAX];
    char temp[] = "temp.txt";

    /* get the input file from the user */
    printf("Enter your input file name:");
    fgets(fname, MAX, stdin);
    fname[strlen(fname) - 1] = '\0';

    /* open input file in read mode */
    fp1 = fopen(fname, "r");

    /* error handling */
    if (!fp1) {
            printf("Unable to open the input file!!\n");
            return 0;
    }

    /* open temporary file in write mode */
    fp2 = fopen(temp, "w");

    /* error handling */
    if (!fp2) {
            printf("Unable to open temporary file!!\n");
            return 0;
    }

    int c = getc(fp1);
    while (c != EOF)
    {
        if (isNum((char) c))
            c = '*';
        putc(c, fp2);
        c = getc(fp1);
    }

    /* close the opened files */
    fclose(fp1);
    fclose(fp2);

    return 0;
}