VC++写二进制文件

[复制链接]
查看11 | 回复1 | 2008-5-11 22:42:47 | 显示全部楼层 |阅读模式
**********以下分别是用CFile实现的文件名及其路径设定、新建文件、写文件、读文件样例代码:///////////////////////////////////////////////////////////////////////////**/ CString DefExt, strFilePath; DefExt.Format("%s","dat文件(*.dat)|*.dat|");CFileDialog dlgFile(FALSE,"dat",NULL,OFN_HIDEREADONLY | OFN_CREATEPROMPT | OFN_NONETWORKBUTTON,DefExt);dlgFile.m_ofn.lpstrTitle="生成二进制DAT文件"; if(dlgFile.DoModal()==IDOK) {strFilePath = dlgFile.GetPathName();} if( strFilePath.IsEmpty() ) {return; }///////////////////////////////////////////////////////////////////////////**/ CFile createTxtFile( strFilePath, CFile::modeCreate | CFile::typeBinary ); createTxtFile.Close();///////////////////////////////////////////////////////////////////////////**/ CFile writeTxtFile( strFilePath, CFile::modeReadWrite | CFile::typeBinary ); intintValueIn = 10000; float fltValueIn = 123.456f; char strValueIn[20] = "乱码luanma"; writeTxtFile.Write( &intValueIn, sizeof(int)); writeTxtFile.Write( &fltValueIn, sizeof(float)); writeTxtFile.Write( &strValueIn, sizeof(strValueIn)); writeTxtFile.Close();///////////////////////////////////////////////////////////////////////////**/ CFile readTxtFile( strFilePath, CFile::modeRead | CFile::typeBinary ); intintValueOut = 0; float fltValueOut = 0; char strValueOut[20] = "\0"; readTxtFile.Read( &intValueOut, sizeof(int)); readTxtFile.Read( &fltValueOut, sizeof(float)); readTxtFile.Read( strValueOut, sizeof(strValueOut)); readTxtFile.Close();**********注:记得每次打开文件,都必须调用Close()释放对文件的占有权。参考资料:分享是件愉悦人心的事情本回答被提问者采纳
回复

使用道具 举报

千问 | 2008-5-11 22:42:47 | 显示全部楼层
FILE *fp = fopen("filename", wb+);fwrite(...);fseek(...);fread(...);printf(...);fclose(fp);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行