试图找到并从文件用C替换字符串 [英] Trying to find and replace a string from file in C

查看:67
本文介绍了试图找到并从文件用C替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    void replaceString(char* file, char* str, char* replace)
    {
        FILE* fp = fopen(file,"rt");
        char buffer[BUFFER];
        while(fgets(buffer,BUFFER,fp)!=NULL)
        {
            char* s;
            s=strstr(buffer,str);
            if(s!=NULL)
            {
                strcpy(s,replace);
                printf("%s is replaced by %s\n",str,replace);
            }
        }
        fclose(fp);
    }
int main(int argc, char **argv)
{
    char* file= "text.txt";
    replaceString(file,"is","was");
    printFile(file);
    return 0;
}

大家好我是新的文件操作,试图找到并替换另一个字符串。请帮忙!我试图打开RT模式下的文件。在一些例如code看到这个。不知道的模式。我猜测,我需要使用TEMP.TXT文件来做到这一点!可以在一个单一的文件来实现,而无需使用任何其他文件??

Guys I am new to file operations, trying to find and replace a string by another. please help! I am trying to open the file in "rt" mode. Saw this in some example code. Not sure about the mode. I am guessing that I need to use a temp.txt file to do that! Can it be done in a single file without using any other file??

推荐答案

下面是一些错误在你的算法。

Here are some of the errors in your algorithm.


  • 您阅读看看在一个时间字符,没有重叠的一个缓冲区。如果 STR 缓冲区之间出现? (即 STR 的第一部分是在一缓冲区的末尾,第二部分是在下一缓冲器的开始)。

  • You read and look at one BUFFER of chars at a time, with no overlap. What if str appears between buffers? (i.e. the first part of str is at the end of a buffer and the second part is at the start of the next buffer).

您尝试覆盖 STR 替换直接使用缓冲区 strcpy的。如果两个字符串有什么不同长度的?如果替换 STR 短,你还是不得不的STR 还有,如果替换较长,它将覆盖文本以下 STR

You try to overwrite str with replace directly in the buffer using strcpy. What if both strings are of different length? If replace is shorter than str, you'd still have the end of str there and if replace is longer, it will overwrite the text following str

即使它们是相同的长度,的strcpy 加在复制的最后决赛0 CHAR(这就是他们告诉你在哪里字符串结束) 。您明确不希望出现这种情况。也许函数strncpy 是一个更好的建议在这里,但如果两个字符串长的不一样它仍然无法正常工作。

Even if they are the same length, strcpy adds the final 0 char at the end of the copy (that's how they tell you where the string ended). you DEFINITIVELY don't want that. Maybe strncpy is a better suggestion here, although it will still not work if both strings aren't the same length.

您替换缓冲区中的字符串,但什么都不做的纠正缓冲!缓冲器不是文件,该文件的内容被复制到缓冲器。所以,你改变了副本,然后什么都没有。该文件将不会改变。你需要写更改到一个文件,preferably不同的功能。

You replace the strings in the buffer but do nothing with the "corrected" buffer! The buffer is not the file, the content of the file was COPIED into the buffer. So you changed the copy and then nothing. The file will not change. You need to write your changes into a file, preferably a different one.

写这样的替代并不像你想象的那么简单。我会尽力帮助你,但它可能是一个有点在你的头上,如果你只是想了解对文件进行处理,但仍与字符串没有完全舒服。

Writing such a replace isn't as trivial as you might think. I may try and help you, but it might be a bit over your head if you're just trying to learn working with files and are still not fully comfortable with strings.

做在一个文件替换是容易的,如果你有足够的内存来读取整个文件一次(如果 BUFFER 比文件大小大),但很棘手如果没有特别的情况下替换长于 STR

Doing the replace in a single file is easy if you have enough memory to read the entire file at once (if BUFFER is larger than the file size), but very tricky if not especially in your case where replace is longer than str.

这篇关于试图找到并从文件用C替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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