VC 怎么把储存路径的字符串中的"\"的变成"\\" ?

来源:百度知道 编辑:UC知道 时间:2024/05/27 11:39:52
比如 str="c:\abc\1.txt"
if(f1.Open(str,CFile::modeCreate|CFile::modeReadWrite))
MessageBox("打开文件失败");
总是提示"打开文件失败"

如何把它变成 "c:\\abc\\1.txt" 使之在文件操作中能正常打开;

我程序的代码:
//listbox中的每一项都是文件路径 比如"c:\abc\1.txt"
//listbox 关联变量定义是CListBox m_list;
void CMyDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
CString str1;
m_list.GetText(m_list.GetCurSel(),str1);
CFile f1;
char ch;
if(f1.Open(str1,CFile::modeCreate|CFile::modeReadWrite))
MessageBox("打开文件失败");
UpdateData(FALSE);
}
我知道原因了 谢谢

"c:\\abc\\1.txt" 中的\\
在变量中存储的实际上只有一个\
只是在程序代码中表示时为了区别于转义字符,而用\\表示
在内存中存储的还是"c:\abc\1.txt"
你后面的代码获得的就是正确的路径,不用改。