CString str;
 str.Format("%s",buf);
    buf的内容为:c:\file\kkk.txt  
    我想建立一个文件夹c:\file,那我就要把c:\file读出来,我该怎么读的,请指点小弟一下,马上给分

解决方案 »

  1.   

    如果你是想只取出目录名,可以用CString的反向查找,找第一个"\",然后用CString的Left方法取左边m个字符就OK了
      

  2.   

    int pos=str.ReverseFind('\\');
    str=str.Left(pos);
      

  3.   

    str[str.ReverseFind('\\')] = 0;
      

  4.   

    我也多次遇到这个问题,一般这样解决:int index = str.ReverseFind('\\');
    if (index > 0) CreateDirectory(str.Left(index+1));
      

  5.   

    //****************************************************************
    //分割pathName使之分为Path,Name,Extension
    //****************************************************************
    BOOL ParsePathName(CString &strPathName, CString& Path,CString &Name, CString &Ext)
    {
    int id=strPathName.GetLength()-1;
    if(id<0)return FALSE;
    Path.Empty();
    Name.Empty();
    Ext.Empty();
    CString FileName;
    while(id>=0&&strPathName[id]!='\\')
    {
    FileName+=strPathName[id];
    id--;
    }
    while(id>=0)
    {
    Path+=strPathName[id];
    id--;
    }
    ConvertString(FileName);
    if(FileName.Find('.')!=-1)
    {
    id=FileName.GetLength()-1;
    while(id>=0&&FileName[id]!='.')
    {
    Ext+=FileName[id];
    id--;
    }
    if(id>0) id--;
    while(id>=0)
    {
    Name+=FileName[id];
    id--;
    }
    }
    else {
    Name=FileName;
    }
    ConvertString(Name);
    ConvertString(Path);
    ConvertString(Ext);
    return TRUE;
    }
    //*********************************************
    //按顺序反转字符串
    //*********************************************
    void ConvertString(CString& str)
    {
    int n=str.GetLength();
    char temp;
    for(int i=0;i<n/2;i++)
    {
    temp=str[i];
    str.SetAt(i,str[n-i-1]);
    str.SetAt(n-i-1,temp);
    }
    }
      

  6.   

    非常谢谢,我明白了,几位大哥,你们真厉害,我还想请教一个问题:
    c:\sunhao\sss\kkk.txt
    我只想要c:\sunhao,(是c:\sunhao\还是c:\sunhao)也就是第一个文件夹,我该怎么办?
      

  7.   

    int pos=str.Find('\\',2);
    str=str.Left(pos);
      

  8.   

    或者
    int pos=str.Find('\\');
    pos=str.Find('\\',pos);
    str=str.Left(pos);
      

  9.   

    或者
    int pos=str.Find('\\');
    pos=str.Find('\\',pos);
    str=str.Left(pos);