CStdioFile file; CString str; file.Open(L"Infor.txt", CFile::modeRead); if(file.Open(_T("Infor.txt"),CFile::modeRead | CFile::typeText)) { while (file.ReadString(str)) { str = str.GetBuffer(); CStringW wstr(str); CStringA astr(wstr); std::string utf8str(astr); vector<string> v = split(utf8str, " ");//分隔符,此处以空格作为分隔符 m_Lisr.InsertItem(i, L""); m_Lisr.SetItemText(i, 0, CA2T(v[0].c_str(), CP_UTF8)); m_Lisr.SetItemText(i, 1, CA2T(v[1].c_str(), CP_UTF8)); m_Lisr.SetItemText(i, 2, CA2T(v[2].c_str(), CP_UTF8)); m_Lisr.SetItemText(i, 3, CA2T(v[3].c_str(), CP_UTF8)); m_Lisr.SetItemText(i, 4, CA2T(v[4].c_str(), CP_UTF8)); m_Lisr.SetItemText(i, 5, CA2T(v[5].c_str(), CP_UTF8)); i++; }
时间: 2024-02-26 18:52:12 浏览: 19
这段代码是使用 MFC 编写的读取文件并分割字符串的程序。首先,打开名为 "Infor.txt" 的文件,以只读方式和文本模式打开。然后,循环读取文件中的每一行,并将其转换为 std::string 类型。接着,使用 split 函数将每一行分割成多个子字符串,并将它们保存在一个 vector 容器中。最后,将这些子字符串插入到一个列表控件中,以便在 GUI 界面中显示。需要注意的是,这段代码使用了一些 MFC 提供的字符串转换函数,例如 CA2T、CW2A 等,用于将 std::string 转换为 CString 类型,并指定字符集为 UTF-8。
相关问题
void CTraverseNetCalcu::OutDataToDAT(CString& str) { IsOutToDAT = 0; CFileDialog dlgFile(FALSE, _T("dat"), NULL, OFN_EXPLORER, _T("(DAT文件)|*.dat")); if (dlgFile.DoModal() == IDCANCEL)return; setlocale(LC_ALL, ""); CString strFileName; strFileName = dlgFile.GetPathName(); CStdioFile SF; if (!SF.Open(strFileName, CFile::modeCreate | CFile::modeWrite)) return;
这段代码实现了一个名为`OutDataToDAT`的函数,用于将数据输出到DAT文件。
首先,将变量`IsOutToDAT`设置为0。
接下来,创建一个文件对话框`dlgFile`,用于选择保存的文件路径和文件名。文件类型被限定为DAT文件。
然后,通过调用`DoModal`函数显示文件对话框,并判断用户是否点击了取消按钮。如果用户取消了选择,则函数返回。
接着,通过调用`setlocale`函数设置当前的本地化环境。
然后,通过调用`GetPathName`函数获取用户选择的文件路径和文件名,并将其存储在变量`strFileName`中。
最后,通过调用`Open`函数打开文件,设置文件的打开模式为创建和写入模式。如果文件打开失败,则函数返回。
CString hotel; CString str; hotel+=m_strHId; hotel+=' '; hotel += m_strHName; hotel += ' '; hotel += m_strHCity; hotel += ' '; wchar_t *wText=CStringToWChar(m_strHName); DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE); char *psText; psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE); hotel+=psText; hotel+=' '; wText=CStringToWChar(m_strHCity); dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE); psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE); hotel+=psText; hotel+=' '; str.Format(L"%d ",m_iHCount); hotel+=str; str.Format(L"%d ",m_iComCount); hotel+=str; str.Format(L"%d ",m_iComEmpCount); hotel+=str; str.Format(L"%d ",m_iComPrice); hotel+=str; str.Format(L"%d ",m_iSupCount); hotel+=str; str.Format(L"%d ",m_iSupEmpCount); hotel+=str; str.Format(L"%d\n",m_iSupPrice); hotel+=str; delete [] wText; delete [] psText; CStdioFile file; file.Open(_T("hotel.txt"),CFile::modeCreate|CFile::modeNoTruncate||CFile::modeWrite); file.SeekToEnd(); file.WriteString(hotel); file.Close(); MessageBox(L"酒店信息添加成功!",L"提示"); CDialogEx::OnOK(); } } void CAddHotelDlg::OnBnClickedCancel() { CDialogEx::OnCancel(); }为什么信息显示不出来
根据你提供的代码,我无法准确判断信息为何无法显示出来。但从代码中可以看出,你将酒店信息写入了一个名为 "hotel.txt" 的文件中,因此可能是读取文件时出现了问题。
你可以尝试检查以下几个方面:
1. 确认文件是否存在,是否有写入权限。
2. 在写入文件之后,尝试读取文件,看看文件中是否真的有写入的信息。
3. 检查程序中读取文件的代码是否正确,包括文件路径、文件打开方式等。
如果以上都没有问题,你可以提供更详细的信息和代码,以便更好地帮助你解决问题。