用fopen打开的文件,如何向其头部加入内容??

cbz 2003-01-20 08:52:17
大家好,我下面的程序有什么不对的吗?
AnsiString buffer = "例子";
int length = strlen(buffer.c_str());
FILE *fp = fopen("c:\\windows\\desktop\\test.txt","r+b");
fseek(fp,0,SEEK_END);
int FileSize = ftell(fp);
fseek(fp,0,SEEK_SET);
char *buffer = new char[FileSize + length];
fread(buffer,1,FileSize,fp);
memmove(buffer + length,buffer,FileSize);
memset(buffer,0,length);
strncpy(buffer,buffer.c_str(),length);
fwrite(buffer,1,length,fp);
delete[] buffer;
fclose(fp);
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingfish 2003-01-20
  • 打赏
  • 举报
回复
fseek(fp,0,SEEK_SET); //加一句
fwrite(buffer,1,length+FileSize,fp); //改这句
hnwlk 2003-01-20
  • 打赏
  • 举报
回复
FILE *fp = fopen("c:\\windows\\desktop\\test.txt","r+b");
改为:
FILE *fp = fopen("c:\\windows\\desktop\\test.txt","rb+");

cbz 2003-01-20
  • 打赏
  • 举报
回复
不行,楼上的,不过还是谢谢
halibut 2003-01-20
  • 打赏
  • 举报
回复
....
char data[]="例子";

int length = strlen(data);
FILE *fp = fopen("c:\\windows\\desktop\\test.txt","r+b");
fseek(fp,0,SEEK_END);
int FileSize = ftell(fp);
fseek(fp,0,SEEK_SET);
char *buffer = new char[FileSize + length];
fread(buffer+length,FileSize,1,fp);
memcpy(buffer,data,length);
fwrite(buffer,length,1,fp);
delete[] buffer
....
cbz 2003-01-20
  • 打赏
  • 举报
回复
我上面的程序就是这样的啊,不过,结果不正确啊,帮看看程序哪里错了
halibut 2003-01-20
  • 打赏
  • 举报
回复
先保存原有的内容呀,写入新内容,再把原有的内容保存回去!

13,873

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧