使用C在文件中替换字符串

时间:2010-04-20 04:39:24

标签: c string replace

我还没有实现这个,我还处于思考阶段,但是我必须通过一个文件并用另一个字符串替换某个字符串。例如,

<img src="/images/logo.gif" ...

应该成为

<img src="/proxy/www.example.com/images/logo.gif" ...

关于我如何处理此问题的任何建议?也许存在一些“字符串替换”C函数可以为我做这个我不知道的...?

现在,如果我必须自己编写这个函数,我会把它作为参数给出文件,要替换的字符串,替换字符串。然后我会手动浏览文件并查找字符串的出现并重新创建它。然而,这看起来非常低效。有更好的方法吗?

谢谢, 赫里斯托斯

7 个答案:

答案 0 :(得分:1)

不,C中没有替换整个文件中的字符串的函数。你必须自己实现它。

那就是说,你向我们展示的是HTML,HTML很棘手,因为它是分层的。你需要正确解析它吗?因为如果你是,那么任务就更难了。看到它是作业,我对此表示怀疑,所以你可能做得足够:

  1. 打开文件并将其加载到内存中(假设它不是太大 - 如果是,可以写入临时文件并在完成后将其移动到原始文件中)
  2. 继续使用strstr查找您需要开始替换的锚字符串
  3. 替换
  4. 重复2和3,直到完成文件
  5. 写回文件

答案 1 :(得分:1)

因为它是家庭作业我假设字符串不能跨越多行。如果这个假设是正确的(并且除了“替换HTML中的文本”的复杂性),那么:

1阅读下一行

2替换字符串并写入行(到另一个文件)

3如果不是结束,请转到#1

4赢\ o /

或者老师想要别的东西耸肩

答案 2 :(得分:1)

首先,C是一种很棒的语言,但却是最痛苦的语言之一。只需要说出来。

您能安全地假设整个文件的内容可以适合内存吗?如果是这样的话:

allocate buffer big enough to hold file contents
read entire file into buffer
inputPtr = 0

while(inputPtr < size of buffer) {
    replacePosition = strstr(inputPtr, stringToReplace);
    if (replacePosition != NULL)
        writeUntil = replacePosition - 1
    else
        writeUntil = end of buffer

    write out buffer from inputPtr to writeUntil inclusive (could be 0 bytes)

    if (replacePosition == NULL) break

    write out the replacement string

    inputPtr = replacePosition + strlen(stringToReplace)
}

答案 3 :(得分:0)

由于这是作业,我不会给你答案,但我会指出一个让人们吵架的经典问题。

在C中,最简单的方法是读取一个固定的字节数(你可以尝试逐行进行,但如果一行太长,则会恢复为读取固定数量的字节)。如果您尝试替换的字符串最终在一个缓冲区和第二个缓冲区之间分割:

buf1 -> "...<img src=\"/ima"
buf2 -> "ges/logo.gif\"..."

你将无法在内存中进行简单的搜索替换。

答案 4 :(得分:0)

你是否尝试使用strcpy函数,

将url分配到一个字符串中,并用strcpy函数替换它。

答案 5 :(得分:0)

您应该调查sed命令。看看它做了什么,做了类似的事情。

它用作过滤器,因此当使用它来替换文件中的某些内容时,您经常要将输出捕获到文件中,然后用新文件替换旧文件。

答案 6 :(得分:0)

您可以使用以下程序进行搜索&amp;替换文件中的字符串。

int main()

{

.....

replaceIPAddress( "System.cfg", "172.16.116.157", "127.0.0.1");

.....

}


void replaceIPAddress( char * confFileName, char *text_to_find , char *text_to_replace  )

{

FILE *input = fopen(confFileName, "r");

FILE *output = fopen("temp.txt", "w");

char buffer[512];


while (fgets(buffer, sizeof(buffer), input) != NULL)
{
    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("temp.txt", confFileName);

}