ChianXu 发表于 2012-08-29 20:21

fopen函数用”a+“打开的问题

各位高手们,求教。
我用fopen(file, "a+")打开一个文件。我的设想是,每次打开文件,不截短。
fread()从文件头开始读。
我想fwrite()也从文件头开始写,我用rewind(),fseek()两个函数都试过了,无法从文件头开始写。是不是a+打开方式限制它一定要从文件尾开始写?
如果我想读写打开文件,并且不截断,读的时候从文件头开始读,写的时候也从文件头开始写,覆盖之前的内容。
该如何设置?

ChianXu 发表于 2012-08-29 20:41

不想打开两次。

xiyoulaoyuanjia 发表于 2012-08-30 11:58

首先fopen 会调用open的,其次fopen 使用"a" 会导致 fopen会每次使用 "O_APPEND"参数调用open的~~   :em03:

xiyoulaoyuanjia 发表于 2012-08-30 12:00

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.

ChianXu 发表于 2012-08-30 12:02

xiyoulaoyuanjia 发表于 2012-08-30 12:00 static/image/common/back.gif


那你说该如何是好呢?又不想打开两次。又想直接write到头部

xiyoulaoyuanjia 发表于 2012-08-30 15:10

直接用系统函数 open write read 试试怎么样 或者修改原先的打开模式~~ :em03:
回复 5# ChianXu


   

ChianXu 发表于 2012-08-30 20:58

回复 6# xiyoulaoyuanjia


    哎,我用最土的办法了,打开一个指针,关闭一个指针。

幸运啤酒盖 发表于 2012-09-02 20:29

fopen(file,"a+")不能重定位位置指针吗?

_Rayx 发表于 2012-09-03 07:59

每次会自动到未尾的。

ChianXu 发表于 2012-09-03 08:18

回复 8# 幸运啤酒盖


    他会强制性的把写指针指向末尾。
页: [1] 2
查看完整版本: fopen函数用”a+“打开的问题