关于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能够改变这种情况,看来是不行的。优先级比较高的还是文件的打开模式啊

 

你可能感兴趣的:(失效,fseek)