c++ 二进制方式读写文件


void CTestFileOperateDlg::OnBnClickedButton1()
{
 FILE *fp1;
 double xc,yc,zc;
 fp1=fopen("result.dat","wb");
 if(!fp1) exit(-1);
 xc=1.0;
 yc=2.0;
 zc=3.0;
 fwrite(&xc,sizeof(double),1,fp1);
 fwrite(&yc,sizeof(double),1,fp1);
 fwrite(&zc,sizeof(double),1,fp1);
 char cData[] = "zxn我的文件数据";
 fwrite(cData,sizeof(cData),1,fp1);
 fflush(fp1);
 fclose(fp1);
 MessageBoxA(NULL,"zxn我的文件数据","写文件成功",MB_OK);
}


void CTestFileOperateDlg::OnBnClickedButton2()
{
 FILE *fp1;
 fp1=fopen("result.dat","rb");
 if(!fp1) exit(-1);
 fseek(fp1,0,SEEK_END);
 int flen=ftell(fp1);
 fseek(fp1,0,0);
 double xc,yc,zc;
 fread(&xc,sizeof(double),1,fp1);
 fread(&yc,sizeof(double),1,fp1);
 fread(&zc,sizeof(double),1,fp1);
 TRACE("%f%f%f",xc,yc,zc);
 char cBuf[100] = {0};
 fread(cBuf,flen - 3*sizeof(double),1,fp1);
 char cMsg[200] = "读到的数据:";
 strcat(cMsg,cBuf);
 MessageBoxA(NULL,cMsg,"info",MB_OK);
 fclose(fp1);
}

你可能感兴趣的:(C++,File,null,FP)