代码之家  ›  专栏  ›  技术社区  ›  BattleTested_закалённый в бою Fritz R.

PathFindExtensionW始终返回“.”而不是完全扩展名

  •  2
  • BattleTested_закалённый в бою Fritz R.  · 技术社区  · 6 年前

    我有以下路径和文件名

    "C:\\Users\\msi\\Desktop\\read-file\\read-file.sdf"
    

    PathFindExtensionW 函数,预期的返回字符串为 ".sdf" 但它回来了 "." 相反!

    这是我的 code :

    #include <stdio.h>
    #include <shlwapi.h>
    
    #define FILENAME        "C:\\Users\\msi\\Desktop\\read-file\\read-file.sdf"                 // current file-path
    #define MAX_FILE_EXT    90                                      // maximum file-extension length
    #define ERR_MSG         "Cannot open the specific file!\n"  // error message if couldn't open the file
    
    #pragma comment(lib, "shlwapi.lib")                             // add this static library for using of PathFindExtension
    
    int main(int argc, char *argv[])
    {
        WIN32_FIND_DATAW data = {0};
        HANDLE fh = 0;
    
        if((FindFirstFile(TEXT(FILENAME), &data)) != INVALID_HANDLE_VALUE)
        {
            WCHAR file_ext[MAX_FILE_EXT] = {0};
            lstrcpy(file_ext, PathFindExtension(TEXT(FILENAME)));
    
            printf("File-extension is : '%s'\n", PathFindExtensionW(TEXT(FILENAME)));
        }
        else
            printf(ERR_MSG);
    
        return 0;
    }
    

    顺便说一下,我用过 wchar_t* ,所以我不得不打电话给 路径查找张力W . 尽管我打过电话 PathFindExtension ,返回相同的结果。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Barmak Shemirani    6 年前

    TEXT L UNICODE

    const wchar_t *wstr = L"this is a wide char string"; //or const WCHAR*, same thing
    

    PathFindExtension PathFindExtensionW

    #ifdef UNICODE
    #define PathFindExtension  PathFindExtensionW
    #else
    #define PathFindExtension  PathFindExtensionA
    #endif // !UNICODE
    

    lstrcpy wcscpy strcpy wcslen strlen wcsxxx strxxx

    "C:\\Users\\msi\\Desktop" SHGetKnownFolderPath

    #include <windows.h>
    #include <stdio.h>
    #include <shlwapi.h>
    #include <Shlobj.h>
    #include <KnownFolders.h>
    
    #pragma comment(lib, "shlwapi.lib")  
    
    int main(void)
    {
        wchar_t desktop[MAX_PATH];
    
        //get desktop path:
        wchar_t *ptr;
        SHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &ptr);
        wcscpy_s(desktop, _countof(desktop), ptr);
        CoTaskMemFree(ptr);
    
        //make filename from desktop path:
        wchar_t filename[MAX_PATH];
        swprintf(filename, _countof(filename), L"%s\\read-file\\read-file.sdf", desktop);
    
        if (PathFileExists(filename))
            wprintf(L"File-extension is : '%s'\n", PathFindExtension(filename));
    
        return 0;
    }
    
        2
  •  1
  •   Student Sneftel    6 年前

    printf wide-string "%ls" "%s"

    wprintf

    WCHAR *wstr = "this is wide string";
    CHAR *str = "this is string";
    
    wprintf("%s", wstr);
    printf("%ls", wstr);
    
    printf("%s", str);