C语言:替换文件中的字符串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void replace(char *path,char *str,char *newstr);
int main(int argc,char *argv[])
{
     if (strcmp(argv[1], "replace") == 0)
        replace(argv[2],argv[3],argv[4]);
    return 0;
}
void replace(char *path,char *str,char *newstr)
{
    FILE *fp;
    fp=fopen(path,"rb+");
    char temp[20];
    fscanf(fp,"%s",temp);
    while(!feof(fp))
    {
        long size=strlen(temp);
        if(strcmp(temp,str)==0)
        {
            strcpy(temp,newstr);
            fseek(fp,-size,SEEK_CUR);
            fprintf(fp,"%s",temp);
        }
        char temp[20]={0};
        fscanf(fp,"%s",temp);
    }
    fclose(fp);
}

要想实现将文本文件中的str替换成newstr,请问上述代码哪里有问题

找到要替换的字符串后,文件指针指向该字符串后面,需要调用fseek重新定位一下文件指针位置,再替换