64,637
社区成员
发帖
与我相关
我的任务
分享
#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
#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