大概过程是这样的,我和远程服务器相连,得到一个文件名称strRead:
BYTE byRecv[MAX_PATH] = {0};
DWORD dwRecv = m_SocketManager.ReadComm( byRecv, MAX_PATH, INFINITE);
CString strRead(byRecv);strRead的字符串类型为"'***.***'"我去掉"'"
strRead.Replace(_T("'"),_T(""));然后得到当前创建的路径为strfilePath :
CString strfilePath = _T("");
strfilePath.Format(_T("%s\\Picture\\%s"), m_strfilePath, strRead);
其中m_strfilePath为exe运行路径然后
CFile  file;
if(file.Open(strfilePath, CFile::modeCreate|CFile::modeWrite))
这里错误,不能创建这个文件,这是为什么?谢谢

解决方案 »

  1.   

    报什么错 strfilePath打印出来看看
      

  2.   

    还是确定strfilePath到底是不是对的,ReadComm读进来的byRecv是什么编码的?unicode还是ansi?如果和当前编码不一致要先转换再赋值给cstring你把strfilePath贴出来看看
      

  3.   


    byRecv是ansi编码的,我的程序是unicode
      

  4.   

    strfilePath.Format(_T("%s\\Picture\\%s"), m_strfilePath, strRead);
    --------------------------
    AfxMessageBox(strfilePath);输出来看看是什么不就清楚了
      

  5.   

    E:\111.bmp
      

  6.   

    你当前工程用的unicode编码?byRecv是不是ansi编码的?假设byRecv是ansi的,那么strfilePath.Format(_T("%s\\Picture\\%s"), m_strfilePath, strRead);得到的编码是有问题的,你必须手动调整byRecv的编码
      

  7.   


    你的byRecv要手动转换成unicode再赋值给CString
      

  8.   


    如何调整呢?byRecv是 ansi
    但CString strRead(byRecv);之后strRead应该是Unicode吧  难道我理解错了?
      

  9.   

    呵呵,可能是我搞错了,你直接用
    CString strfilePath=_T("E:\111.bmp");打开文件能行吗?
      

  10.   

    我这儿用 BYTE byRecv[MAX_PATH] = "11.bmp";
    CString strRead(byRecv); CString strfilePath = _T("");
    strfilePath.Format(_T("c:\\%s"), strRead); CFile file;
    if(file.Open(strfilePath, CFile::modeCreate|CFile::modeWrite))
    {
    int i = 0;
    }测试时没问题的,一直能打开
      

  11.   

    文件操作的地方写成这样比较好
    try
    {
     CFile file;
     file.Open(...);
     ...
    }
    catch(CFileException* e)
    {
     e->ReportError();
     e->Delete();
    }
      

  12.   

    会不会在strRead.Replace(_T("'"),_T(""));
    后把字符串结束标志给改了
      

  13.   

    你在文件打开前面把字符串的内容复制出来看看TCHAR tmp[1024];
    _tcscpy(tmp,strfilePath.GetBuffer());
    CFile file;
    if(file.Open(strfilePath, CFile::modeCreate|CFile::modeWrite))比如我这儿是,'.'也是wchar
    - tmp 0x0039e610 "c:\11.bmp" wchar_t [1024]
    [0] 99 L'c' wchar_t
    [1] 58 L':' wchar_t
    [2] 92 L'\' wchar_t
    [3] 49 L'1' wchar_t
    [4] 49 L'1' wchar_t
    [5] 46 L'.' wchar_t
    [6] 98 L'b' wchar_t
    [7] 109 L'm' wchar_t
    [8] 112 L'p' wchar_t
    [9] 0 wchar_t
      

  14.   


    非常感谢您,我找到错误了 ,现在已经ok了  以前那个字符地址中有\r\n作为结束符,这个字符导致地址无效。结贴了