up up

解决方案 »

  1.   

    文件名的路径都知道 了,你还懒啊。
    应该没有的吧
    1.CString strFile = "C:\\1.txt";int nFind = strFile.ReverseFind('\\');
    strFile = strFile.Left(nFind);
    2.string strFile = "C:\\1.txt";
    int nFind = strFile.find_last_of('\\');
    strFile = strFile.substr(0,nFind);
      

  2.   

    *strrchr(filename,'\\')=0;
    //filename现在只有目录了。
      

  3.   

    TCHAR szPath[_MAX_PATH] = _T("F:\\MyProject\\DEMO\\Debug\\DEMO.exe");
    TCHAR szDrive[_MAX_DRIVE] = {0};
    TCHAR szDir[_MAX_DIR] = {0};
    TCHAR szName[_MAX_FNAME] = {0};
    TCHAR szExt[_MAX_EXT] = {0};
    _tsplitpath(szPath, szDrive, szDir, szName, szExt); CString strText(_T(""));
    strText += szDrive;
    strText += szDir;
    AfxMessageBox(strText);
      

  4.   

    原来还可以_tsplitpath啊
    我每次都是cstring find 来find去的
      

  5.   

    从文件Handle得到文件名,仅供参考!
    源自网络#include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <string.h>
    #include <psapi.h>
    #include <strsafe.h>
    #define BUFSIZE 512BOOL GetFileNameFromHandle(HANDLE hFile) 
    {
    BOOL bSuccess = FALSE;
    TCHAR pszFilename[MAX_PATH+1];
    HANDLE hFileMap;// Get the file size.
    DWORD dwFileSizeHi = 0;
    DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
    {
         printf("Cannot map a file with a length of zero.\n");
         return FALSE;
    }// Create a file mapping object.
    hFileMap = CreateFileMapping(hFile, 
                        NULL, 
                        PAGE_READONLY,
                        0, 
                        1,
                        NULL);if (hFileMap) 
    {
        // Create a file mapping to get the file name.
        void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);    if (pMem) 
        {
          if (GetMappedFileName (GetCurrentProcess(), 
                                 pMem, 
                                 pszFilename,
                                 MAX_PATH)) 
          {        // Translate path with device name to drive letters.
            TCHAR szTemp[BUFSIZE];
            szTemp[0] = '\0';        if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) 
            {
              TCHAR szName[MAX_PATH];
              TCHAR szDrive[3] = TEXT(" :");
              BOOL bFound = FALSE;
              TCHAR* p = szTemp;          do 
              {
                // Copy the drive letter to the template string
                *szDrive = *p;            // Look up each device name
                if (QueryDosDevice(szDrive, szName, MAX_PATH))
                {
                  UINT uNameLen = _tcslen(szName);              if (uNameLen < MAX_PATH) 
                  {
                    bFound = _tcsnicmp(pszFilename, szName, 
                        uNameLen) == 0;                if (bFound) 
                    {
                      // Reconstruct pszFilename using szTempFile
                      // Replace device path with DOS path
                      TCHAR szTempFile[MAX_PATH];
                      StringCchPrintf(szTempFile,
                                MAX_PATH,
                                TEXT("%s%s"),
                                szDrive,
                                pszFilename+uNameLen);
                      StringCchCopyN(pszFilename, MAX_PATH+1, szTempFile, _tcslen(szTempFile));
                    }
                  }
                }            // Go to the next NULL character.
                while (*p++);
              } while (!bFound && *p); // end of string
            }
          }
          bSuccess = TRUE;
          UnmapViewOfFile(pMem);
        }    CloseHandle(hFileMap);
    }
    _tprintf(TEXT("File name is %s\n"), pszFilename);
    return(bSuccess);//http://hi.baidu.com/tr0j4n
    }
    int _tmain(int argc, TCHAR *argv[])
    {
        HANDLE hFile;    if( argc != 2 )
        {
            printf("This sample takes a file name as a parameter.\n");
            return 0;
        }
        hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
            OPEN_EXISTING, 0, NULL);    if(hFile == INVALID_HANDLE_VALUE)
        {
            printf("CreateFile failed with %d\n", GetLastError());
            return 0;
        }
        GetFileNameFromHandle( hFile );
    }
      

  6.   

    问题最好描述清楚一些吧...如果是文件名里面就带着目录的..LZ也不会问了吧..望LZ补充清楚问题.