想实现数据库文件的操作,可以从文件中间读写
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("data.dbf","rb+"); //使用"rb+"模式,可以往半中间插入数据,而且是覆盖插入,若使用"ab+"每次都插入到最后面,若使用"wb+"则直接重建一个空文件并覆盖原文件,这点要小心
if(NULL != fp)
{
if(-1 == (fseek(fp,9, SEEK_SET)))
printf("seek error\n");
fwrite("abcde",1, 5, fp);
fclose(fp);
}
else
{
printf("fopen error");
return 0;
}
return 0;
}
开始使用”rb+”做读测试,一切ok,然后用”wb+”做块修改操作,发现原文件变成0kb大小
使用”rb+”实现文件的块修改,在linux和Windows下均测试通过。
附fopen用法
格式:FILE * fopen(const char * path,const char * mode)
参数: path:需要打开的文件路径
mode:文件打开方式
r(read): 读
w(write): 写
a(append): 追加
t(text): 文本文件,可省略不写
b(banary): 二进制文件
+: 读和写
留言
You really make it seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!