关于fseek失效的一种情况

 

最近在试一下fseek这个函数,发现总是不能达到目的,fseek根本就是无效,所用的IDE环境是VC6

采用的C文件格式

void main()

{

FILE *pfile = fopen("a.txt","a+");

fputs("i love you\n",pfile);

fseek(pfile,0,SEEK_SET);

fwrite("here is write\n",1,sizeof("here is write\n"),pfile);

fclose(pfile);

}


这个总是失败,“here is write”没有写到文件开头,在文件结尾,返回的fseek为0


今天又重新找了一个例子,发现文件打开方式有问题。


void main()

{

FILE *pfile = fopen("a.txt","w");

fputs("i love you\n",pfile);

fseek(pfile,0,SEEK_SET);

fwrite("here is write\n",1,sizeof("here is write\n"),pfile);

fclose(pfile);

}


原来原因在于读写方式,看来还要认真学习一下了啊

查了一下fopen的a+

"a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.


原来是a模式是追加方式写文件,fseek是无效的,虽然知道a是追加模式,可是,我以为fseek能够改变这种情况,看来是不行的。优先级比较高的还是文件的打开模式啊

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值