文件读写操作

文件操作一般使用MFC的CFile类。

写文件相对比较简单,即使文件不存在,也可以新建:

CFile file( s1, CFile::modeCreate|CFile::modeWrite );	
file.Write( m_InfoList, sizeof(INFOLIST_DATA)*LISTNUMDATA);
file.Close();

读文件要相对复杂一些,因为首先要确认文件存在才行,否则就会发生未知错误。

strFileTitle = "Parameter.bin";//文件名
bFlag=0;
if( finder.FindFile(strFileTitle) )
{	
    //找到文件,打开文件			
    CFile file( strFileTitle, CFile::modeRead );
    DWORD dwCount=0;
    dwCount = file.GetLength();			    	
	dwCount = file.Read( pFileBuf, dwConnt );	//读取文件		
	if( dwCount != dwFileLen )
	{
		//读取失败,置文件异常标志1
		bFlag = 1;				
	}	  
    //关闭文件
    file.Close();
}
else
{
    //文件丢失,置文件异常标志2
    bFlag = 2;		
}

你可能感兴趣的:(文件)