读书人

fseek函数用在文本文件中没用 怎么用?

发布时间: 2012-03-19 22:03:05 作者: rapoo

fseek函数用在文本文件中没用 如何用???高手指点下
我的思路是 首先查看文件 "** "是否存在,若不存在则新建力个并输出 "****..NNNN "若文件存在则用feek函数使文件内部指针从文件尾部后移4个字节 既移动到NNNN的前面~然后进行写~下面是我编的程序~:
if(fp2=fopen(filename[2], "r ")==NULL)
{ fclose(fp2);
fp=fopen(filename[2], "a+ ");
fputs( "ZCZC 325\n ",fp);
fputs( "NNNN ",fp);
}

else
{
fclose(fp2);
fp=fopen(filename[2], "a+ ");
fseek( "fp,-4L,SEEK_END);
}
fprintf(fp, "sdfa %d\n ",50);
输出结果是
ZCZC 325
NNNNsdfa50

我想要的结果是
ZCZC 325
sdfa50
NNNN
麻烦那位高手大哥大姐讲解下 谢谢~




[解决办法]
When a file is opened with the "a " or "a+ " access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
[解决办法]
以a、a+模式打开文件,所有的写操作都发生在文件的末尾,使用fseek函数可以使文件指针指向不同的地方,但是当发生写操作时,文件指针总是指向文件的末尾,所以文件的数据是不能覆盖的。
建议楼主以其它方式打开文件‘w+’

读书人网 >C语言

热点推荐