替换模式“查找"的 C 程序用“替换"在文本文件中作为输入和输出文件应该具有替换模式 [英] C program to replace a pattern "find" with "replace" in a textfile as input and output file should have the replaced pattern

查看:46
本文介绍了替换模式“查找"的 C 程序用“替换"在文本文件中作为输入和输出文件应该具有替换模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索,但没有得到如何去做的确切想法,因为我能够编写的代码只是针对该模式的一次出现,但是如果该模式有不同的出现行??

i searched on net but not getting the exact idea of how to do it as the code m being able to write is just for a single occurence of the pattern but if there are different lines of occurence of that pattern??

推荐答案

如果您使用的是 Unix 或类似系统(如 Linux 或 MacOS X),那么您已经有一个命令行程序来执行此操作:sed.

If you are on a Unix or similar system (like Linux or MacOS X) then you already have a command line program to do this: sed.

否则你必须从原始文件中读取并写入一个新文件,在读写时替换文本.之后,您必须将新文件重命名为旧的原始文件.

Otherwise you have to read from the original file and write to a new file, replacing the text while reading and writing. After that you have to rename the new file as the old original file.

至于文本的实际查找,如果它是一个固定字符串,您可以使用例如strstr,其他明智的查看正则表达式.

As for the actual finding of the text, if it's a fixed string you can use e.g. strstr, other wise look into regular expressions.

如何使用 sed(1):

$ sed -i 's/xyz/abc/g' infile.txt

上面的命令将读取infile.txt,用abc替换所有出现的文本xyz并将其写回infile.txt.

The above command will read infile.txt, replace all occurrences of the text xyz with abc and write it back to infile.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");

此代码已经测试可以正常工作.

注意:如果你不知道指针运算是什么,那么上面的代码可能很难理解和理解,你只需要相信我它做它应该做的.:)

Note: If you don't know what pointer arithmetic is, then the above code might be very hard to follow and understand, and you just have to trust me that it does what it should. :)

这篇关于替换模式“查找"的 C 程序用“替换"在文本文件中作为输入和输出文件应该具有替换模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆