Reputation: 21
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".
Upvotes: 0
Views: 7614
Reputation: 490338
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.
Upvotes: 3