使用fopen的a+参数遇到的问题

在《UNIX环境高级编程》这本书中,关于a+的解释是这样的:为在文件尾读和写而打开或创建。也就是说是在文件尾部读和写。最近需要写一下程序,打开一个10M大小的文件,随机位置写入一串数据。当时我就是用的a+,结果调了好久,即使使用fseek调整位置,但是每次都是在文件末尾添加内容,后来换成了r+才解决。所以特意又man了一下fopen,结果发现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. 也就是说,实际上a+打开的文件读是从开头开始读,而写 只能在文件末尾。而且写是不能通过fseek调整位置的,读是可以通过fseek调整位置的。也不知道是《UNIX环境高级编程》有错误还是翻译的问题,或者是标准不同。反正感觉这个地方说的不太对。

猜你喜欢

转载自blog.csdn.net/qu1993/article/details/82762110
今日推荐