C程序替换一个模式;查找“;加上;替换“;在文本文件中,作为输入和输出的文件应具有替换的模式
我在网上搜索了一下,但没有得到如何做的确切想法,因为我能写的代码只针对该模式的一次出现,但如果该模式出现了不同的行???如果您在Unix或类似系统上(如Linux或MacOS X)那么您已经有了一个命令行程序来执行此操作:C程序替换一个模式;查找“;加上;替换“;在文本文件中,作为输入和输出的文件应具有替换的模式,c,replace,C,Replace,我在网上搜索了一下,但没有得到如何做的确切想法,因为我能写的代码只针对该模式的一次出现,但如果该模式出现了不同的行???如果您在Unix或类似系统上(如Linux或MacOS X)那么您已经有了一个命令行程序来执行此操作:sed 否则,您必须读取原始文件并写入新文件,在读取和写入时替换文本。之后,您必须将新文件重命名为旧的原始文件 至于文本的实际查找,如果它是一个固定字符串,您可以使用例如strstr,从其他角度查看 编辑:如何使用: 上述命令将读取infle.txt,将所有出现的文本xyz替换
sed
否则,您必须读取原始文件并写入新文件,在读取和写入时替换文本。之后,您必须将新文件重命名为旧的原始文件
至于文本的实际查找,如果它是一个固定字符串,您可以使用例如strstr
,从其他角度查看
编辑:如何使用:
上述命令将读取infle.txt
,将所有出现的文本xyz
替换为abc
,并将其写回infle.txt
编辑:如何搜索/替换:
FILE *input = fopen("input.txt", "r");
FILE *output = fopen("temp.txt", "w");
char buffer[512];
while (fgets(buffer, sizeof(buffer), input) != NULL)
{
/* The text to find */
static const char text_to_find[] = "xyz";
/* The text to replace it with */
static const char text_to_replace[] = "abc";
char *pos = strstr(buffer, text_to_find);
if (pos != NULL)
{
/* Allocate memory for temporary buffer */
char *temp = calloc(
strlen(buffer) - strlen(text_to_find) + strlen(text_to_replace) + 1, 1);
/* Copy the text before the text to replace */
memcpy(temp, buffer, pos - buffer);
/* Copy in the replacement text */
memcpy(temp + (pos - buffer), text_to_replace, strlen(text_to_replace));
/* Copy the remaining text from after the replace text */
memcpy(temp + (pos - buffer) + strlen(text_to_replace),
pos + strlen(text_to_find),
1 + strlen(buffer) - ((pos - buffer) + strlen(text_to_find)));
fputs(temp, output);
free(temp);
}
else
fputs(buffer, output);
}
fclose(output);
fclose(input);
/* Rename the temporary file to the original file */
rename("input.txt", "temp.txt");
此代码已被删除
注意:如果您不知道指针算术是什么,那么上面的代码可能很难理解和理解,您只需相信我,它会做它应该做的事情。:) 如果您在Unix或类似系统(如Linux或MacOS X)上,那么您已经有了执行此操作的命令行程序:
sed
否则,您必须读取原始文件并写入新文件,在读取和写入时替换文本。之后,您必须将新文件重命名为旧的原始文件
至于文本的实际查找,如果它是一个固定字符串,您可以使用例如strstr
,从其他角度查看
编辑:如何使用:
上述命令将读取infle.txt
,将所有出现的文本xyz
替换为abc
,并将其写回infle.txt
编辑:如何搜索/替换:
FILE *input = fopen("input.txt", "r");
FILE *output = fopen("temp.txt", "w");
char buffer[512];
while (fgets(buffer, sizeof(buffer), input) != NULL)
{
/* The text to find */
static const char text_to_find[] = "xyz";
/* The text to replace it with */
static const char text_to_replace[] = "abc";
char *pos = strstr(buffer, text_to_find);
if (pos != NULL)
{
/* Allocate memory for temporary buffer */
char *temp = calloc(
strlen(buffer) - strlen(text_to_find) + strlen(text_to_replace) + 1, 1);
/* Copy the text before the text to replace */
memcpy(temp, buffer, pos - buffer);
/* Copy in the replacement text */
memcpy(temp + (pos - buffer), text_to_replace, strlen(text_to_replace));
/* Copy the remaining text from after the replace text */
memcpy(temp + (pos - buffer) + strlen(text_to_replace),
pos + strlen(text_to_find),
1 + strlen(buffer) - ((pos - buffer) + strlen(text_to_find)));
fputs(temp, output);
free(temp);
}
else
fputs(buffer, output);
}
fclose(output);
fclose(input);
/* Rename the temporary file to the original file */
rename("input.txt", "temp.txt");
此代码已被删除
注意:如果您不知道指针算术是什么,那么上面的代码可能很难理解和理解,您只需相信我,它会做它应该做的事情。:) 你现在有可以逐行读取文本文件的代码吗?请澄清:你是在搜索固定文本还是模式?您需要正则表达式引擎吗?您有访问正则表达式库的权限吗?您使用的是哪个平台/编译器?到目前为止你都尝试了什么?没有,我正在寻找一个使用unix平台的固定文本。假设我的文件是:1 xyz 23 2 xyz 45,那么输出应该是1 abc 23 2 abc45@cdarke当前位置我尝试了一个代码,但我认为这里面有问题,实际上我不知道“c”文件命令m在c++中更为出色。有人能提供基于此示例输出的完整代码吗?如我上面所述?您现在有可以逐行读取文本文件的代码吗?请澄清:您是在搜索固定文本还是模式?您需要正则表达式引擎吗?您有访问正则表达式库的权限吗?您使用的是哪个平台/编译器?到目前为止你都尝试了什么?没有,我正在寻找一个使用unix平台的固定文本。假设我的文件是:1 xyz 23 2 xyz 45,那么输出应该是1 abc 23 2 abc45@cdarke当前位置我尝试了一个代码,但我认为这里面有问题,实际上我不知道“c”文件命令我在c++中更擅长,有人能提供基于这个示例输出的完整代码吗,如我上面所说的吗?非常感谢,你的代码正是我想要的..非常感谢:):)很好,但不幸的是,中的代码唯一的问题是它可以替换一个单词的单个实例。@kingsmasher1只需在循环中调用
strstrstr
,while(pos!=NULL){/*all other code*/pos=strstr(pos,text_to_find);}
非常感谢你的代码完全符合我的要求..非常感谢:):)很棒,但不幸的是,中的代码唯一的问题是它可以替换单词的单个实例。@kingsmasher1只需在循环中调用strstrstr
:while(pos!=NULL){/*all-other-code*/pos=strstr(pos,text_to_find);}