为什么a +模式不适用于此?

时间:2014-09-14 16:50:48

标签: io d

根据官方D书:

  

a +:读取并附加访问权限。       如果该文件不存在,则将其创建为空       如果文件已经存在,则保留其内容并打开文件以从头开始读取并在结尾写入。

但如果我执行以下操作,则不会从文件中读取任何内容:

import std.stdio;

void main() {
    File fh = File("/tmp/x.mail", "a+");
    string line;
    while((line = fh.readln()) !is null) {
        write(line);
    }
}

我在OSX 10.9上,编译器是ldc2。

1 个答案:

答案 0 :(得分:4)

来自MacOS X documentation for fopen()

"a+" Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.

我很确定Phobos在MacOS X的幕后使用fopen()来打开该文件,因此流被定位在最后。尝试调用fseek()然后阅读。

相关问题