gpt4 book ai didi

c - 如何在从另一个文件复制时编辑文件

转载 作者:行者123 更新时间:2023-11-30 20:25:02 25 4
gpt4 key购买 nike

如何在从另一个文件复制内容的同时编辑文件

source = fopen("sourceFile.txt", "r");
if( source == NULL )
{
printf("Error in doStepOneAndTwo, can't open file source \n");
return USERERR;
}

target = fopen("targetFile.txt", "w");
if( target == NULL )
{
fclose(source);
printf("Error in doStepOneAndTwo, can't open file target %s \n",str);
return USERERR;
}

while( ( ch = fgetc(source) ) != EOF )
{
fputc(ch, target);
//Here I need to check if ch == "blah" change it to "twoBlah" and save twoBlah to targetFile.txt instead of blah
}

我的语法有问题

最佳答案

const char *search_word = "blah";
const char *replace_word = "twoBlah";
const char *p = search_word;

while(1){
ch = fgetc(source);
if(*p == ch){
++p;
if(!*p){//match!
fprintf(target, "%s", replace_word);
p = search_word;
}
} else {
if(p != search_word){
const char *temp = search_word;
while(temp != p)
fputc(*temp++, target);
p = search_word;
}
if(ch == EOF)
break;
fputc(ch, target);
}
}

关于c - 如何在从另一个文件复制时编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105735/

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