如果找到了一个词,那么如何在另一个文件中替换它?
If a word has been found then how to replace it in another file?
我有这段代码,为了便于理解,我将其分为两部分。第一个选项工作正常并向我显示要更新的详细信息。
我对第二部分有问题,其中代码正确地从文件中获取 name/word,但我不知道如何将其替换为在另一个文件中新建 name/word?因为我搜索了很多,你能帮我一些代码吗?谢谢!
这里是file.txt
bilalkhan 20/20/1980 908732343
这里有代码
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void firstWord(char str[]){
int i = 0;
while(!isspace(str[i])){
i++;
}
str[i]='[=11=]';
}
void hello(){
int option;
char updated_name[50], read[100];
short int FLAG = 0;
static const char * listing[] = {"Name", "Date of birth","ID card number"};
FILE * fr3 = fopen("file.txt","r");
FILE * fw1 = fopen("new.txt","w");
if (fr3 == NULL || fw1 == NULL) {
perror("Unable to read text file.");
exit(0);
}
for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option)
printf("%d. Your %s\n", option, listing[option-1]);
SELECT 更新选项
fputs("Select your choice to update: ", stdout);
scanf("%d", &option);
char string[100];
if (option == 1){
while(fgets(string, 100, fr3) != NULL){
firstWord(string);
printf("'%s' found. Now replace it with another name: ", string);
scanf("%s", &updated_name);
// Here I want to update the name but don't know the code.
fclose(fr3);
exit(0);
}
}
fclose(fw1);
}
int main(){ hello(); }
只需将更新后的名称写入输出文件即可。
if (option == 1){
while(fgets(string, 100, fr3) != NULL){
firstWord(string);
printf("'%s' found. Now replace it with another name: ", string);
scanf("%s", &updated_name);
fprintf(fw1, "%s\n", updated_name);
fclose(fr3);
fclose(fw1);
exit(0);
}
}
fclose(fw1);
您实际上并没有替换任何东西,您只是要求一个新名称并将其放入文件中。此过程中未使用旧名称。
我有这段代码,为了便于理解,我将其分为两部分。第一个选项工作正常并向我显示要更新的详细信息。
我对第二部分有问题,其中代码正确地从文件中获取 name/word,但我不知道如何将其替换为在另一个文件中新建 name/word?因为我搜索了很多,你能帮我一些代码吗?谢谢!
这里是file.txt
bilalkhan 20/20/1980 908732343
这里有代码
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void firstWord(char str[]){
int i = 0;
while(!isspace(str[i])){
i++;
}
str[i]='[=11=]';
}
void hello(){
int option;
char updated_name[50], read[100];
short int FLAG = 0;
static const char * listing[] = {"Name", "Date of birth","ID card number"};
FILE * fr3 = fopen("file.txt","r");
FILE * fw1 = fopen("new.txt","w");
if (fr3 == NULL || fw1 == NULL) {
perror("Unable to read text file.");
exit(0);
}
for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option)
printf("%d. Your %s\n", option, listing[option-1]);
SELECT 更新选项
fputs("Select your choice to update: ", stdout);
scanf("%d", &option);
char string[100];
if (option == 1){
while(fgets(string, 100, fr3) != NULL){
firstWord(string);
printf("'%s' found. Now replace it with another name: ", string);
scanf("%s", &updated_name);
// Here I want to update the name but don't know the code.
fclose(fr3);
exit(0);
}
}
fclose(fw1);
}
int main(){ hello(); }
只需将更新后的名称写入输出文件即可。
if (option == 1){
while(fgets(string, 100, fr3) != NULL){
firstWord(string);
printf("'%s' found. Now replace it with another name: ", string);
scanf("%s", &updated_name);
fprintf(fw1, "%s\n", updated_name);
fclose(fr3);
fclose(fw1);
exit(0);
}
}
fclose(fw1);
您实际上并没有替换任何东西,您只是要求一个新名称并将其放入文件中。此过程中未使用旧名称。