string replace in file using C

2019-07-27 12:03发布

I haven't yet implemented this, I'm still in the thinking stage, but I have to go through a file and replace a certain string with another string. For example,

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

should become

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

Any advice on how I can approach this? Perhaps there exist some "string replace" C functions that would do this for me that I don't know about...?

Right now, if I had to write this function myself, I would give it as parameters the file, string to replace, replacement string. Then I would manually go through the file and look for an occurrence of the string and recreate it. This, however, seems very inefficient. Are there better ways to do this?

Thanks, Hristo

标签: c string replace
7条回答
爷的心禁止访问
2楼-- · 2019-07-27 12:22

First of all, C is an awesome language, but is one of the most painful languages to do this type of operation in. Just had to say it.

Can you safely assume that the contents of the entire file can fit in memory? If so:

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)
}
查看更多
Evening l夕情丶
3楼-- · 2019-07-27 12:35

Since it's homework I'm going with the assumption that the string can not span multiple lines. If this assumption is correct (and barring the complications with "replacing text in HTML") then:

1 Read the next line

2 Replace string and write line (to another file)

3 If not at end, goto #1

4 Win \o/

Or perhaps the teacher wants something else shrug

查看更多
我命由我不由天
4楼-- · 2019-07-27 12:36

You should investigate the sed command. See what it does and do something similar.

It works as a filter, so when using it to replace something in a file what you often do is capture the output into a file and then replace the old file with the new file.

查看更多
欢心
5楼-- · 2019-07-27 12:41

Are you try strcpy function for this,

Assign the url in one string and replace it by strcpy function.

查看更多
小情绪 Triste *
6楼-- · 2019-07-27 12:45

No, there is no function in C that replaces a string throughout a file. You must implement it yourself.

That said, what you're showing us is HTML, and HTML is tricky, because it's hierarchical. Are you required to correctly parse it? Because if you are, the task is much more difficult. Seeing that it's homework, I doubt it, so you might do enough by:

  1. open the file and load it to memory (assuming it isn't too large - if it is, you can write into a temporary file and move it onto the original one after you've finished)
  2. continuously use strstr to find the anchor string you need to start replacing
  3. replace
  4. repeat 2 and 3 until finished with file
  5. write file back
查看更多
倾城 Initia
7楼-- · 2019-07-27 12:45

You can use following program to search & replace string in a file.

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);

}
查看更多
登录 后发表回答