如何在fclose之前把fwrite的内容刷新到文件中去?

micosun6 2008-10-21 06:35:28
我用fflush也没用?
...全文
228 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
micosun6 2008-10-22
  • 打赏
  • 举报
回复
谢谢!
太乙 2008-10-21
  • 打赏
  • 举报
回复 1
上面我举得例子不恰当,下面:



#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp,*fp2;
char msg[6];
if ((fp=fopen("1.txt","w+"))==NULL)
{
printf("open error!");
}
fwrite("hello",5,1,fp);
fflush(fp);
if ((fp2=fopen("1.txt","r"))==NULL)
{
printf("open error!");
}
fread(msg,5,1,fp2);
msg[5]='\0';
printf(msg);
fclose(fp);
}


output:

helloPress any key to continue



---------------------------------------------------------------------
如果去掉fflush():


#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp,*fp2;
char msg[6];
if ((fp=fopen("1.txt","w+"))==NULL)
{
printf("open error!");
}
fwrite("hello",5,1,fp);
// fflush(fp);
if ((fp2=fopen("1.txt","r"))==NULL)
{
printf("open error!");
}
fread(msg,5,1,fp2);
msg[5]='\0';
printf(msg);
fclose(fp);
}

output:

烫烫蘌ress any key to continue


Just4life 2008-10-21
  • 打赏
  • 举报
回复
学习了
太乙 2008-10-21
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>



void main()
{
FILE *fp;
char msg[6];
if ((fp=fopen("1.txt","w+"))==NULL)
{
printf("open error!");
}
fwrite("hello",5,1,fp);
fflush(fp);
fseek(fp,0,SEEK_SET);
fread(msg,5,1,fp);
msg[5]='\0';
printf(msg);
fclose(fp);
}
output:
helloPress any key to continue







micosun6 2008-10-21
  • 打赏
  • 举报
回复
直接写到文件??
wuyu637 2008-10-21
  • 打赏
  • 举报
回复
fclose();
帅得不敢出门 2008-10-21
  • 打赏
  • 举报
回复
直接写到文件中不就行了.
帅得不敢出门 2008-10-21
  • 打赏
  • 举报
回复
此刷新与此 fflush 不是同一个概念
sunhuanwen 2008-10-21
  • 打赏
  • 举报
回复
没用,文件句柄不关闭文件中是不会写入数据的。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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