关于对宽字符的读写

非常蛋疼的过程,具体的细节还得继续学习.
1,文件的写
void Cibm_testDlg::OnBnClickedButtonSave()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString defFileName=CString("configure.ini");
	CFileDialog fileDlg(false,L"ini",defFileName);
	fileDlg.m_ofn.lpstrTitle=L"Save Configure File:";
	fileDlg.m_ofn.lpstrFilter=L"configure files(.ini)\0*.ini\0\0";
	CString path;
	GetModuleFileName(NULL,path.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	path=path.Left (path.ReverseFind ('\\'))+L"\\";
	fileDlg.m_ofn.lpstrInitialDir=path;
	CString str;
	if(IDOK==fileDlg.DoModal())
	{
		//这里对宽字符的读写,需要一些特殊的处理
		CStdioFile file(fileDlg.GetPathName(),CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);
		char fx[2]; //增加特殊的标志,表示是Unicode编码
		fx[0]=0xFF;
		fx[1]=0xFE;
		file.Write(fx,2);
		str.Format(L"%d",isAutoGeneration);
		//回车符的添加
		str=str+L"\r\n";
		file.WriteString(str);
		str.Format(L"%d",isAutoModification);
		str=str+L"\r\n";
		file.WriteString(str);
		str.Format(L"%d",totalFileSize);
		str=str+L"\r\n";
		file.WriteString(str);
		str.Format(L"%d",perFileSize);
		str=str+L"\r\n";
		file.WriteString(str);
		file.Close();
	}
}

2,文件的读
CString path;
GetModuleFileName(NULL,path.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
path=path.Left (path.ReverseFind ('\\'))+L"\\configure.ini";
CStdioFile file;
if(!file.Open(path,CFile::modeRead|CFile::typeBinary)) 
{
    MessageBox(L"Unable to open configure file\n");
}
else
{
    char fx[2];
    file.Read(fx,2);
    CString str;
    file.ReadString(str);
    isAutoGeneration=_ttoi(str);
    file.ReadString(str);
    isAutoModification=_ttoi(str);
    file.ReadString(str);
    totalFileSize=_ttoi(str);
    file.ReadString(str);
    perFileSize=_ttoi(str);
}

你可能感兴趣的:(读写)