使用&QUOT的; R + QUOT;在FOPEN在Windows VS Linux的 [英] The use of "r+" in fopen on windows vs linux

查看:108
本文介绍了使用&QUOT的; R + QUOT;在FOPEN在Windows VS Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与一些code这是打开,读取和修改的文本文件玩弄各地。快速(简体)的例子是:

I was toying around with some code which was opening, reading, and modifying a text file. A quick (simplified) example would be:

#include <stdio.h>
int main()
{
    FILE * fp = fopen("test.txt", "r+");
    char line[100] = {'\0'};
    int count = 0;
    int ret_code = 0;
    while(!feof(fp)){
        fgets(line, 100, fp);
        // do some processing on line...
        count++;
        if(count == 4) {
          ret_code = fprintf(fp, "replaced this line\n");
          printf("ret code was %d\n", ret_code);
          perror("Error was: ");
        }
    }
    fclose(fp);
    return 0;
}

现在在Linux上,用gcc编译(4.6.2)这个code运行,并修改文件的5日线。同样的code,Windows7的上编译运行使用Visual C ++ 2010运行并声称已成功(报告的19个字符返回code和 PERROR 说: 没有错误),但未能更换就行了。

Now on Linux, compiled with gcc (4.6.2) this code runs, and modifies the file's 5th line. The same code, running on Windows7 compiled with Visual C++2010 runs and claims to have succeeded (reports a return code of 19 characters and perror says "No error") but fails to replace the line.

在Linux上我的文件具有完全的权限:

On Linux my file has full permissions:

-rw-rw-rw- 1 mike users 191 Feb 14 10:11 test.txt

而据我可以告诉它是相同的Windows:

And as far as I can tell it's the same on Windows:

test.txt的(右键) - >属性 - >安全性结果
允许是检查读取和放大器;写用户,系统管理员和

test.txt (right click) -> properties -> Security
"Allow" is checked for Read & Write for user, System, and Admin.

我得到使用Windows上的MinGW的GCC相同的结果,所以我知道这不是一个Visual C ++的功能。

I get the same results using MinGW's gcc on Windows so I know it's not a Visual C++ "feature".

我失去了一些东西明显,或者说是事实,我没有错误,也没有输出只是使用一个未公开的功能 R + fopen()函数在Windows?

Am I missing something obvious, or is the fact that I get no errors, but also no output just an undocumented "feature" of using r+ with fopen() on Windows?

编辑:结果似乎甚至在的微软的网站他们说R +应该打开阅读的的书面方式。他们还提出了这样一个字条:


Seems even at Microsoft's site they say "r+" should open for reading and writting. They also made this note:

当R +,W +,或指定为A +访问类型,读,写都可以使用(该文件被认为是打开更新)。然而,当你阅读和写作之间切换,必须有一个中间fflush,fsetpos,fseek的,或快退操作。当前位置可以为fsetpos或fseek的操作中指定,如果需要的话。

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

所以,我想:

        ...
        if(count == 4) {
          fflush(fp);
          ret_code = fprintf(fp, "replaced this line\n");
          fflush(fp);
          printf("ret code was %d\n", ret_code);
          ...

无济于事。

推荐答案

按照 Linux手册页为 fopen()函数

读取和写入,可以在任何顺序读/写流混合。
  需要注意的是ANSI C要求文件定位功能干预
  输出和输入之间,除非输入操作遇到
  档案结尾。 (如果这一条件不能满足,则读是允许
  返回比最近的其它写入的结果)。因此,它
  是很好的做法(实际上有时是在Linux下有必要)将
  一个fseek的(3)或fgetpos(3)写入和之间的操作读取操作
  在这样的流。此操作可以是一个明显的无操作(如在
  fseek的(...,0L,SEEK_CUR)要求其同步的副作用。

Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek(3) or fgetpos(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.

所以,你应该始终调用 fseek的()(如,例如 fseek的(...,0,SEEK_CUR))从一个文件阅读和写作之间切换时。

So, you should always call fseek() (as, eg. fseek(..., 0, SEEK_CUR)) when switching between reading and writing from a file.

这篇关于使用&QUOT的; R + QUOT;在FOPEN在Windows VS Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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