Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何替换文本文件中的精确单词_C - Fatal编程技术网

C 如何替换文本文件中的精确单词

C 如何替换文本文件中的精确单词,c,C,当我打开临时文件时,我看到单词被替换了。主文件中的字符串打印在临时文件中,不带任何空格 我怎样才能解决这个问题 while (!feof(ptr2)) { strcpy(string, "\0"); fscanf(ptr2, "%s", string); if (!strcmp(string, word)) // If match found num++; if (strstr(string, word)) { r2 =

当我打开临时文件时,我看到单词被替换了。主文件中的字符串打印在临时文件中,不带任何空格

我怎样才能解决这个问题

while (!feof(ptr2))
{
    strcpy(string, "\0");
    fscanf(ptr2, "%s", string);
    if (!strcmp(string, word)) // If match found
        num++;

    if (strstr(string, word))
    {
        r2 = string;
        while (r1 = strstr(r2, word))
        {
            while (r2 != r1)
            {
                fputc(*r2, temp);
                r2++;
            }
            r1 = r1 + strlen(word);
            fprintf(temp, "%s", word1);
            r2 = r1;
        }
        while (*r2 != '\0') {
            fputc(*r2, temp);
            r2++;
        }
    }
    else {
        fputs(string, temp);
    }

请参阅。
“%s”
跳过所有空白;如果希望在输出中看到空白,则必须输出空白。你需要考虑什么时候需要空白,什么时候需要换行。@JonathanLeffler-有更简短的版本吗?(oo)@Nguaial:有什么的较短版本吗?@nguai:链接的较短版本是标题-当(!feof(fp))时不要使用
。请参阅。
“%s”
跳过所有空白;如果希望在输出中看到空白,则必须输出空白。你需要考虑什么时候需要空白,什么时候需要换行。@JonathanLeffler-有更简短的版本吗?(oo)@Nguaial:有什么的较短版本吗?@nguai:链接的较短版本是标题-当(!feof(fp))
时不要使用