C语言 以“a+”模式打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3645123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:22:52  来源:igfitidea点击:

Opening a file in 'a+ 'mode

cfilefile-iofopenfile-pointer

提问by codaddict

If a file is opened using the following command:

如果使用以下命令打开文件:

FILE *f1=fopen("test.dat","a+");

The man page reads:

手册页写道:

a+

Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

一个+

打开以进行读取和追加(在文件末尾写入)。如果文件不存在,则创建该文件。读取的初始文件位置在文件的开头,但输出总是附加到文件的末尾。

So does f1have 2 separate offset pointers, one for read & another for write?

那么是否f1有 2 个单独的偏移指针,一个用于读取,另一个用于写入?

回答by codaddict

No.

不。

There is just one pointer which initially is at the start of the file but when a write operation is attempted it is moved to the end of the file. You can reposition it using fseekor rewindanywhere in the file for reading, but writing operations will move it back to the end of file.

只有一个指针最初位于文件的开头,但是当尝试进行写操作时,它会移动到文件的末尾。您可以使用fseekrewind文件中的任何位置重新定位它以进行读取,但写入操作会将其移回文件末尾。

回答by raj_arni

No it has only one pointer.

不,它只有一个指针。

回答by R.. GitHub STOP HELPING ICE

You can nevermix reading and writing operations on a FILEwithout calling fseekin between. It may work as you wish on some implementations, but a program that depends on this has undefined behavior. Thus the questions of having 2 positions is meaningless.

永远不能FILE没有调用的情况下混合读写操作fseek。它可能会在某些实现上按您的意愿工作,但是依赖于此的程序具有未定义的行为。因此,拥有 2 个职位的问题是没有意义的。