gpt4 book ai didi

c - 使用c程序一次尝试用星号替换文件中的所有数字(不是特定数字)?

转载 作者:行者123 更新时间:2023-11-30 16:26:06 24 4
gpt4 key购买 nike

如何将包含数字和单词的文件中的所有数字更改为星号?我的源代码只允许我一次更改一次,而且很忙。在这段代码中,我已经插入了打开的文件,替换并关闭文件。供您引用,这是我作为一名大学生的第一年项目,我还不擅长用 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;
}

最佳答案

如果您要替换字符,请不要通过编写替换单词的程序来使工作变得困难,从而在程序中造成不必要的复杂性。下面是一个非常简单的方法。

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

关于c - 使用c程序一次尝试用星号替换文件中的所有数字(不是特定数字)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53153849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com