VC中通过CFile类保存结构体AAA中的变量到本地。在线等...... 如题VC中通过CFile类保存结构体AAA中的变量到本地,并可以读取本地的文件给结构体的变量的代码怎么写?高结贴率,有可用的马上结贴! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 typedef struct _AA { int a; int b; int c;}AA;// Write filetry { AA info = {1,2,3}; CFile file; file.Open(_T("F:\\11.txt"), CFile::modeCreate|CFile::modeWrite); file.Write(&info, sizeof(info)); file.Close(); } catch (CFileException* e) { e->ReportError(); e->Delete(); }// Read filetry { AA info = {0}; CFile file; file.Open(_T("F:\\11.txt"), CFile::modeRead); file.Read(&info, sizeof(info)); file.Close(); CString str; str.Format(_T("a = %d, b = %d, c = %d"), info.a, info.b, info.c); AfxMessageBox(str); } catch (CFileException* e) { e->ReportError(); e->Delete(); } 1、建议:多看msdn2、用CFile打开文件:TCHAR* pszFileName = _T("c:\\test\\myfile.dat");CFile myFile;CFileException fileException;if ( !myFile.Open( pszFileName, CFile::modeCreate | CFile::modeReadWrite, &fileException ) ){ TRACE( _T("Can't open file %s, error = %u\n"), pszFileName, fileException.m_cause );} 3、读写文件:TCHAR szBuffer[256]; UINT nActual = 0; CFile myFile;if ( myFile.Open( _T("c:\\test\\myfile.dat"), CFile::modeCreate | CFile::modeReadWrite, NULL ) ){ myFile.Write( szBuffer, sizeof( szBuffer ) ); myFile.Flush(); myFile.Seek( 0, CFile::begin ); nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); }其中的szBuffer当然可以是你的结构体地址4、关闭文件:myFile.Close();5、附送一个:HANDLE hFile = CreateFile(_T("CFile_File.dat"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE){ AfxMessageBox(_T("Couldn't create the file!"));}else{ // Attach a CFile object to the handle we have. CFile myFile(hFile); static const TCHAR sz[] = _T("I love CFile!"); // write string myFile.Write(sz, sizeof(sz)); // We can call Close() explicitly, but the destructor would have // also closed the file for us. Note that there's no need to // call the CloseHandle() on the handle returned by the API because // MFC will close it for us. myFile.Close();}以上全部来自msdn 正解,另外结构体AAA中不能有指针. 直接把AAA以自己的规则序列化到文件,比如用空格来隔开结构体AAA中的每个变量,读取的时候进行反向操作 WTL窗口中的WS_TABSTOP怎么失灵了? InternetFindNextFile的问题 DCOM的应用 请问在MFC中如何刷新屏幕的? 回车换行问题 求助:请各位赐教推广卡尔曼滤波算法在vc中的实现或代码 大侠们进来帮帮忙吧 急求! 一个最难的问题—关于ActiveX控件背景色透明的问题 [小白来了]新手基础问题,送分了! MFC访问access数据库神奇问题 求助,关于CArchive读取CObArray的问题
typedef struct _AA
{
int a;
int b;
int c;
}AA;// Write file
try
{
AA info = {1,2,3};
CFile file;
file.Open(_T("F:\\11.txt"), CFile::modeCreate|CFile::modeWrite);
file.Write(&info, sizeof(info));
file.Close();
}
catch (CFileException* e)
{
e->ReportError();
e->Delete();
}// Read file
try
{
AA info = {0};
CFile file;
file.Open(_T("F:\\11.txt"), CFile::modeRead);
file.Read(&info, sizeof(info));
file.Close(); CString str;
str.Format(_T("a = %d, b = %d, c = %d"), info.a, info.b, info.c);
AfxMessageBox(str);
}
catch (CFileException* e)
{
e->ReportError();
e->Delete();
}
2、用CFile打开文件:
TCHAR* pszFileName = _T("c:\\test\\myfile.dat");
CFile myFile;
CFileException fileException;if ( !myFile.Open( pszFileName, CFile::modeCreate |
CFile::modeReadWrite, &fileException ) )
{
TRACE( _T("Can't open file %s, error = %u\n"),
pszFileName, fileException.m_cause );
}
3、读写文件:
TCHAR szBuffer[256];
UINT nActual = 0;
CFile myFile;if ( myFile.Open( _T("c:\\test\\myfile.dat"), CFile::modeCreate |
CFile::modeReadWrite, NULL ) )
{
myFile.Write( szBuffer, sizeof( szBuffer ) );
myFile.Flush();
myFile.Seek( 0, CFile::begin );
nActual = myFile.Read( szBuffer, sizeof( szBuffer ) );
}其中的szBuffer当然可以是你的结构体地址4、关闭文件:
myFile.Close();
5、附送一个:
HANDLE hFile = CreateFile(_T("CFile_File.dat"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Couldn't create the file!"));
}
else
{
// Attach a CFile object to the handle we have.
CFile myFile(hFile); static const TCHAR sz[] = _T("I love CFile!"); // write string
myFile.Write(sz, sizeof(sz)); // We can call Close() explicitly, but the destructor would have
// also closed the file for us. Note that there's no need to
// call the CloseHandle() on the handle returned by the API because
// MFC will close it for us.
myFile.Close();
}
以上全部来自msdn