Opening a file in 'a+ 'mode

烈酒焚心 提交于 2019-11-28 07:23:23

问题


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?


回答1:


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 or rewind anywhere in the file for reading, but writing operations will move it back to the end of file.




回答2:


No it has only one pointer.




回答3:


You can never mix reading and writing operations on a FILE without calling fseek 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.



来源:https://stackoverflow.com/questions/3645123/opening-a-file-in-a-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!