c中的feof错误循环

发布于 2024-11-19 17:13:10 字数 630 浏览 2 评论 0原文

我使用下面的代码从文件中读取一个字符并将其替换为另一个字符, 但我在转到文件末尾时遇到了 error.loop 。

怎么了?

我在 Linux(netbeans IDE)上测试了这段代码,它是正确的并且工作得很好,但是当我尝试在 Windows 中使用 VS 2008 时,我发现了一个非结束循环。

//address = test.txt

FILE *fp;
fp=fopen(address,"r+");
if(fp == 0)
{
    printf("can not find!!");
}
else
{
    char w = '0';  /// EDIT : int w;
    while(1)
    {
        if((w = fgetc(fp)) != EOF)
        {
            if((w = fgetc(fp)) != EOF)
            {
                fseek(fp,-2,SEEK_CUR);
                fprintf(fp,"0");
            }
        }
        else
        {
            break;
        }
    }
} 
fclose(fp);

I use below code to read a char from file and replace it with another,
but I have an error.loop in going to end of file.

What is wrong?

I tested this code on linux (netbeans IDE) and it was correct and worked beautiful but when I tried to use VS 2008 in windows , I found a non end loop.

//address = test.txt

FILE *fp;
fp=fopen(address,"r+");
if(fp == 0)
{
    printf("can not find!!");
}
else
{
    char w = '0';  /// EDIT : int w;
    while(1)
    {
        if((w = fgetc(fp)) != EOF)
        {
            if((w = fgetc(fp)) != EOF)
            {
                fseek(fp,-2,SEEK_CUR);
                fprintf(fp,"0");
            }
        }
        else
        {
            break;
        }
    }
} 
fclose(fp);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-11-26 17:13:10

您将 fgetc 的结果存储在一个 char,而不是一个 int。

char w = '0'; /* Wrong, should be int. */

顺便说一下,C FAQ中提到了这个问题。

如果类型 charunsigned,则实际的
EOF 值将被截断(通过具有
其高阶位被丢弃,
可能会导致 255 或 0xff)和
不会被识别为 EOF,
导致有效无限
输入

编辑

再次阅读您的问题,您查找两个字符并写一个字符的方式非常可疑。这很可能导致无限循环。

EDIT2

你(可能)想要这样的东西(未经测试):

while ((w = getc(fp)) != EOF) {
    fseek(fp, -1, SEEK_CUR);
    fprintf(fp, "0");
    fflush(fp); /* Apparently necessary, see the answer of David Grayson. */
}

You are storing the result of fgetc in a char, instead of an int.

char w = '0'; /* Wrong, should be int. */

Incidentally, this problem is mentioned in the C FAQ.

If type char is unsigned, an actual
EOF value will be truncated (by having
its higher-order bits discarded,
probably resulting in 255 or 0xff) and
will not be recognized as EOF,
resulting in effectively infinite
input
.

EDIT

Reading your question again, it's highly fishy the way you seek back two characters and write one character. That could well lead to an infinite loop.

EDIT2

You (likely) want something like this (untested):

while ((w = getc(fp)) != EOF) {
    fseek(fp, -1, SEEK_CUR);
    fprintf(fp, "0");
    fflush(fp); /* Apparently necessary, see the answer of David Grayson. */
}
梦里兽 2024-11-26 17:13:10

cplusplus.com 上的 fopen 文档 说:

对于同时读取和读取的模式
允许写入(或附加)
(那些包含“+”号的),
流应该被刷新(fflush)或
重新定位(fseek、fsetpos、倒回)
读操作之间
随后是写操作或
写操作后跟
读取操作。

我们可以在 fprintf 之后添加一个 fflush 调用来满足该要求。

这是我的工作代码。它创建一个名为 example.txt 的文件,程序退出后该文件的内容将为 000000000000n

#include <stdio.h>

int main(int argc, char **argv)
{
    FILE * fp;
    int w;

    fp = fopen("example.txt","w");
    fprintf(fp, "David Grayson");
    fclose(fp);

    fp = fopen("example.txt","r+");
    while(1)
    {
        if((w = fgetc(fp)) != EOF)
        {
            if((w = fgetc(fp)) != EOF)
            {
                fseek(fp,-2,SEEK_CUR);
                fprintf(fp,"0");
                fflush(fp);  // Necessary!
            }
        }
        else
        {
            break;
        }
    }
    fclose(fp);
}

这是在 Windows 中使用 MinGW 进行测试的。

The fopen documentation on cplusplus.com says:

For the modes where both read and
writing (or appending) are allowed
(those which include a "+" sign), the
stream should be flushed (fflush) or
repositioned (fseek, fsetpos, rewind)
between either a reading operation
followed by a writing operation or a
writing operation followed by a
reading operation.

We can add an fflush call after the fprintf to satisfy that requirement.

Here is my working code. It creates a file named example.txt and after the program exits that file's contents will be 000000000000n.

#include <stdio.h>

int main(int argc, char **argv)
{
    FILE * fp;
    int w;

    fp = fopen("example.txt","w");
    fprintf(fp, "David Grayson");
    fclose(fp);

    fp = fopen("example.txt","r+");
    while(1)
    {
        if((w = fgetc(fp)) != EOF)
        {
            if((w = fgetc(fp)) != EOF)
            {
                fseek(fp,-2,SEEK_CUR);
                fprintf(fp,"0");
                fflush(fp);  // Necessary!
            }
        }
        else
        {
            break;
        }
    }
    fclose(fp);
}

This was tested with MinGW in Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文