本文介绍了如何提取文件名,不与GetFullPathName文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提取路径字符串的文件名,但我与GetFullPathName功能的困难:

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

每一次消息框显示C:\\示例\\ 0。0而不是一个文件名,例如的text.txt

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

推荐答案

我修改您的code为简单一点:

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;
}

和运行它自身的源$ C ​​$ C,结果如下:

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

这是说,我想知道为什么你在所有会跟 GetFullPathName 一塌糊涂。在评论你说你得到的文件名 GetOpenFileName 。这意味着你要在一个 OPENFILENAME 结构中的文件信息。这既包括 lpstrFile ,它具有完整路径的文件, lpstrFileTitle 该文件的名称不带路径信息 - 这正是你似乎有什么想

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.

这篇关于如何提取文件名,不与GetFullPathName文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:44