读取到文件末尾后,fputs返回-1

Mic*_*zek 4 c file-io stream

我在Windows上遇到文件写入失败的问题.我把它简化为这个例子:

FILE* f = fopen("test.out", "r+b");
fseek(f, -1, SEEK_END); // one byte before the end
printf("read byte: %c\n", fgetc(f)); // read the last byte; now at the end
printf("attempting write: %d\n", fputs("text", f));
Run Code Online (Sandbox Code Playgroud)

这正确输出了最后一个字节test.out,但fputs失败并返回-1.这些类似的例子都很好:

  • 不读

    FILE* f = fopen("test.out", "r+b");
    fseek(f, 0, SEEK_END); // this is where I ended up after the fgetc() above
    printf("attempting write: %d\n", fputs("text", f));
    
    Run Code Online (Sandbox Code Playgroud)
  • 阅读后寻求结束(即使我们已经在那里)

    FILE* f = fopen("test.out", "r+b");
    fseek(f, -1, SEEK_END);
    printf("read byte: %c\n", fgetc(f));
    fseek(f, 0, SEEK_END);
    printf("attempting write: %d\n", fputs("text", f));
    
    Run Code Online (Sandbox Code Playgroud)
  • 寻求我们已经确切的位置

    FILE* f = fopen("test.out", "r+b");
    fseek(f, -1, SEEK_END);
    printf("read byte: %c\n", fgetc(f));
    fseek(f, ftell(f), SEEK_SET);
    printf("attempting write: %d\n", fputs("text", f));
    
    Run Code Online (Sandbox Code Playgroud)
  • 读,但不是最后一个字节

    FILE* f = fopen("test.out", "r+b");
    fseek(f, -2, SEEK_END); // two bytes before the end
    printf("read byte: %c\n", fgetc(f)); // read the penultimate byte
    printf("attempting write: %d\n", fputs("text", f));
    
    Run Code Online (Sandbox Code Playgroud)
  • 阅读结束(...)

    FILE* f = fopen("test.out", "r+b");
    fseek(f, -1, SEEK_END); // one byte before the end
    printf("read byte: %c\n", fgetc(f)); // read the last byte; now at the end
    printf("read byte: %c\n", fgetc(f)); // read a garbage byte
    printf("attempting write: %d\n", fputs("text", f));
    
    Run Code Online (Sandbox Code Playgroud)

这些似乎都表明流错误或EOF的问题,但ferror(f)feof(f)都返回0,直到失败fputs().之后,ferror(f)非零,但是errno为0,所以我不知道问题是什么

我在Visual Studio 2008和GCC 4.7.2(MinGW)中仅在Windows上看到了这一点.在Linux上,相同的代码运行没有错误

tor*_*rek 6

C标准要求您在从"读取模式"切换到"写入模式"时进行搜索,反之亦然,除非在某些通常不值得枚举的特殊情况之后.

实现(例如我多年前为BSD或Linux编写的实现)可以比要求更宽容,使您的代码"正常工作".(这非常简单,你只需要在实现中保留两个独立的计数器而不是单个组合计数器.)但是标准并不要求实现友好,而Windows不需要.