打开“a”中的文件;模式
如果使用以下命令打开文件:
FILE *f1=fopen("test.dat","a+");
手册页显示:
a+
打开以进行读取和追加(写入文件末尾)。这 如果文件不存在则创建该文件。初始文件位置 读取位于文件的开头,但输出是 始终附加到文件末尾。
那么 f1
是否有 2 个独立的偏移指针,一个用于读取和一个偏移指针。另一个用于写?
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 f1
have 2 separate offset pointers, one for read & another for write?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不会。
只有一个指针最初位于文件的开头,但是当尝试执行写入操作时,它会移至文件末尾。您可以在文件中的任何位置使用
fseek
或rewind
重新定位它以进行读取,但写入操作会将其移回文件末尾。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
fseek
orrewind
anywhere in the file for reading, but writing operations will move it back to the end of file.不,它只有一个指针。
No it has only one pointer.
您永远不能在
FILE
上混合读取和写入操作,而无需在其间调用fseek
。它可能在某些实现上按您希望的方式工作,但是依赖于此的程序具有未定义的行为。因此,拥有2个位置的问题是没有意义的。You can never mix reading and writing operations on a
FILE
without callingfseek
in 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.