How to extract the file name without folder path with GetFullPathName

旧时模样 提交于 2019-12-25 18:24:52

问题


I want to extract the file name from path string but i have difficulties with the GetFullPathName Function:

WCHAR *fileExt;
WCHAR szDir[256]; //dummy buffer
GetFullPathNameW(g_fileName,256, szDir,&fileExt); //g_filename is filename with path string
swprintf(szDestDir, L"C:\\Example\\%s", fileExt);
MessageBoxW(hwnd,szDestDir,L"Debug",MB_OK); //debug message

every time the message box displays "C:\Example\0" with 0 instead a filename, for example "text.txt".


回答1:


I modified your code a little bit for simplicity:

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv) { 
    char *fileExt;
    char szDir[256]; //dummy buffer
    GetFullPathName(argv[0], 256, szDir, &fileExt); 
    printf("Full path: %s\nFilename: %s", szDir, fileExt);
    return 0;
}

And ran it on its own source code, with the following results:

C:\C\source>trash9 trash9.cpp
Full path: C:\C\source\trash9
Filename: trash9

That said, I have to wonder why you'd mess with GetFullPathName at all. In the comments you say you're getting the file name GetOpenFileName. This means you're getting the file information in an OPENFILENAME structure. This includes both lpstrFile, which has the full path to the file, and lpstrFileTitle which has the file name without path information -- exactly what you seem to want.



来源:https://stackoverflow.com/questions/17632117/how-to-extract-the-file-name-without-folder-path-with-getfullpathname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!