请问win32编程怎么实现MFC中的 getfilepath

BlackHamlet 2017-11-13 06:23:23
我之前有个程序
里面遍历了文件夹,然后把所有文件的路径跟文件名加入容器
获取路径用的是getfilepath方法
每次查找下一个文件,然后调用getfilepath就可以获取找到的文件的路径

我现在要用win32接口来改写这个程序
遇到这里卡住了
我用_getcwd,好像只能获取当前主程序所在的目录
如何能做到和getfilepath类似的效果呢?


我目前是用_finddata_t这个类来查找文件的,可以获取到文件名,但是这个类并没有路径的参数啊 =-=
...全文
258 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2017-11-18
  • 打赏
  • 举报
回复
MFC环境下在getfilepath下断点F11进去可以看看它的实现 最终也都是基于win32的封装
赵4老师 2017-11-17
  • 打赏
  • 举报
回复
10楼正解!
赵4老师 2017-11-16
  • 打赏
  • 举报
回复
GetFullPathName The GetFullPathName function retrieves the full path and filename of a specified file. DWORD GetFullPathName( LPCTSTR lpFileName, // pointer to name of file to find path for DWORD nBufferLength, // size, in characters, of path buffer LPTSTR lpBuffer, // pointer to path buffer LPTSTR *lpFilePart // pointer to filename in path ); Parameters lpFileName Pointer to a null-terminated string that specifies a valid filename. This string can use either short (the 8.3 form) or long filenames. nBufferLength Specifies the size, in characters, of the buffer for the drive and path. lpBuffer Pointer to a buffer that contains the null-terminated string for the name of the drive and path. lpFilePart Pointer to a variable that receives the address (in lpBuffer) of the final filename component in the path. Return Values If the GetFullPathName function succeeds, the return value is the length, in characters, of the string copied to lpBuffer, not including the terminating null character. If the lpBuffer buffer is too small, the return value is the size of the buffer, in characters, required to hold the path. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The GetFullPathName function merges the name of the current drive and directory with the specified filename to determine the full path and filename of the specified file. It also calculates the address of the filename portion of the full path and filename. This function does not verify that the resulting path and filename are valid or that they refer to an existing file on the associated volume. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetShortPathName, GetTempPath, SearchPath
shen_wei 2017-11-16
  • 打赏
  • 举报
回复
WINAPI DWORD GetModuleFileName( HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
  • 打赏
  • 举报
回复
char chpath[MAX_PATH];
			GetModuleFileName(NULL,(LPSTR)chpath,sizeof(chpath));
BlackHamlet 2017-11-14
  • 打赏
  • 举报
回复
引用 3 楼 jszj 的回复:
这个路径 要自己组装。 在开始搜索时,你需要传入一个搜索的路径,这个路径就很重要,需要记录起来,这里暂时记为 P1 在搜索到的每一个目录或者文件,就加到这个P1的后面, 比如一个文件的路径 P1\F1 比如一个目录的路径 P1\S1 在S1下继续搜索,比如再搜索到一个 文件,则 P1\S1\F2 目录,则 P1\S1\S2 就这样递归搜索并把每一级的目录以后最后的文件名连接起来就成了整个路径
我绕了一下路,用组合方法得到了路径。那么我想知道的是,win32接口中,要做类似的工作只能自己编程吗?没有方便的标准库对不对?
red-fly 2017-11-14
  • 打赏
  • 举报
回复
这个路径 要自己组装。 在开始搜索时,你需要传入一个搜索的路径,这个路径就很重要,需要记录起来,这里暂时记为 P1 在搜索到的每一个目录或者文件,就加到这个P1的后面, 比如一个文件的路径 P1\F1 比如一个目录的路径 P1\S1 在S1下继续搜索,比如再搜索到一个 文件,则 P1\S1\F2 目录,则 P1\S1\S2 就这样递归搜索并把每一级的目录以后最后的文件名连接起来就成了整个路径
zgl7903 2017-11-14
  • 打赏
  • 举报
回复

/* FFIND.C: This program uses the 32-bit _find functions to print
 * a list of all files (and their attributes) with a .C extension
 * in the current directory.
 */

#include <stdio.h>
#include <io.h>
#include <time.h>

void main( void )
{
    struct _finddata_t c_file;
    long hFile;

    /* Find first .c file in current directory */
    if( (hFile = _findfirst( "*.c", &c_file )) == -1L )
       printf( "No *.c files in current directory!\n" );
   else
   {
            printf( "Listing of .c files\n\n" );
            printf( "\nRDO HID SYS ARC  FILE         DATE %25c SIZE\n", ' ' );
            printf( "--- --- --- ---  ----         ---- %25c ----\n", ' ' );
            printf( ( c_file.attrib & _A_RDONLY ) ? " Y  " : " N  " );
            printf( ( c_file.attrib & _A_SYSTEM ) ? " Y  " : " N  " );
            printf( ( c_file.attrib & _A_HIDDEN ) ? " Y  " : " N  " );
            printf( ( c_file.attrib & _A_ARCH )   ? " Y  " : " N  " );
            printf( " %-12s %.24s  %9ld\n",
               c_file.name, ctime( &( c_file.time_write ) ), c_file.size );

            /* Find the rest of the .c files */
            while( _findnext( hFile, &c_file ) == 0 )
            {
                printf( ( c_file.attrib & _A_RDONLY ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_SYSTEM ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_HIDDEN ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_ARCH )   ? " Y  " : " N  " );
                printf( " %-12s %.24s  %9ld\n",
                   c_file.name, ctime( &( c_file.time_write ) ), c_file.size );
            }

       _findclose( hFile );
   }
}



red-fly 2017-11-14
  • 打赏
  • 举报
回复
引用 4 楼 u011648839 的回复:
[quote=引用 3 楼 jszj 的回复:] 这个路径 要自己组装。 在开始搜索时,你需要传入一个搜索的路径,这个路径就很重要,需要记录起来,这里暂时记为 P1 在搜索到的每一个目录或者文件,就加到这个P1的后面, 比如一个文件的路径 P1\F1 比如一个目录的路径 P1\S1 在S1下继续搜索,比如再搜索到一个 文件,则 P1\S1\F2 目录,则 P1\S1\S2 就这样递归搜索并把每一级的目录以后最后的文件名连接起来就成了整个路径
我绕了一下路,用组合方法得到了路径。那么我想知道的是,win32接口中,要做类似的工作只能自己编程吗?没有方便的标准库对不对?[/quote] 我没用过其它的搜索类的接口,所以不太清楚详细情况,看楼上的,似乎是fullpath一类的函数,可以研究一下
赵4老师 2017-11-14
  • 打赏
  • 举报
回复
_fullpath, _wfullpath Create an absolute or full path name for the specified relative path name. char *_fullpath( char *absPath, const char *relPath, size_t maxLength ); wchar_t *_wfullpath( wchar_t *absPath, const wchar_t *relPath, size_t maxLength ); Function Required Header Compatibility _fullpath <stdlib.h> Win 95, Win NT _wfullpath <stdlib.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to a buffer containing the absolute path name (absPath). If there is an error (for example, if the value passed in relPath includes a drive letter that is not valid or cannot be found, or if the length of the created absolute path name (absPath) is greater than maxLength) the function returns NULL. Parameters absPath Pointer to a buffer containing the absolute or full path name relPath Relative path name maxLength Maximum length of the absolute path name buffer (absPath). This length is in bytes for _fullpath but in wide characters (wchar_t) for _wfullpath. Remarks The _fullpath function expands the relative path name in relPath to its fully qualified or “absolute” path, and stores this name in absPath. A relative path name specifies a path to another location from the current location (such as the current working directory: “.”). An absolute path name is the expansion of a relative path name that states the entire path required to reach the desired location from the root of the filesystem. Unlike _makepath, _fullpath can be used to obtain the absolute path name for relative paths (relPath) that include “./” or “../” in their names. For example, to use C run-time routines, the application must include the header files that contain the declarations for the routines. Each header file include statement references the location of the file in a relative manner (from the application’s working directory): #include <stdlib.h> when the absolute path (actual file system location) of the file may be: \\machine\shareName\msvcSrc\crt\headerFiles\stdlib.h _fullpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wfullpath is a wide-character version of _fullpath; the string arguments to _wfullpath are wide-character strings. _wfullpath and _fullpath behave identically except that _wfullpath does not handle multibyte-character strings. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tfullpath _fullpath _fullpath _wfullpath If the absPath buffer is NULL, _fullpath calls malloc to allocate a buffer of size _MAX_PATH and ignores the maxLength argument. It is the caller’s responsibility to deallocate this buffer (using free) as appropriate. If the relPath argument specifies a disk drive, the current directory of this drive is combined with the path. Example /* FULLPATH.C: This program demonstrates how _fullpath * creates a full path from a partial path. */ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <direct.h> char full[_MAX_PATH], part[_MAX_PATH]; void main( void ) { while( 1 ) { printf( "Enter partial path or ENTER to quit: " ); gets( part ); if( part[0] == 0 ) break; if( _fullpath( full, part, _MAX_PATH ) != NULL ) printf( "Full path is: %s\n", full ); else printf( "Invalid path\n" ); } } File Handling Routines See Also _getcwd, _getdcwd, _makepath, _splitpath
赵4老师 2017-11-14
  • 打赏
  • 举报
回复
_makepath, _wmakepath Create a path name from components. void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext ); void _wmakepath( wchar_t *path, const wchar_t *drive, const wchar_t *dir, const wchar_t *fname, const wchar_t *ext ); Routine Required Header Compatibility _makepath <stdlib.h> Win 95, Win NT _wmakepath <stdlib.h> or <wchar.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters path Full path buffer drive Drive letter dir Directory path fname Filename ext File extension Remarks The _makepath function creates a single path and stores it in path. The path may include a drive letter, directory path, filename, and filename extension. _wmakepath is a wide-character version of _makepath; the arguments to _wmakepath are wide-character strings. _wmakepath and _makepath behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tmakepath _makepath _makepath _wmakepath The following arguments point to buffers containing the path elements: drive Contains a letter (A, B, and so on) corresponding to the desired drive and an optional trailing colon. _makepath inserts the colon automatically in the composite path if it is missing. If drive is a null character or an empty string, no drive letter and colon appear in the composite path string. dir Contains the path of directories, not including the drive designator or the actual filename. The trailing slash is optional, and either a forward slash (/) or a backslash (\) or both may be used in a single dir argument. If a trailing slash (/ or \) is not specified, it is inserted automatically. If dir is a null character or an empty string, no slash is inserted in the composite path string. fname Contains the base filename without any extensions. If fname is NULL or points to an empty string, no filename is inserted in the composite path string. ext Contains the actual filename extension, with or without a leading period (.). _makepath inserts the period automatically if it does not appear in ext. If ext is a null character or an empty string, no period is inserted in the composite path string. The path argument must point to an empty buffer large enough to hold the complete path. Although there are no size limits on any of the fields that constitute path, the composite path must be no larger than the _MAX_PATH constant, defined in STDLIB.H. _MAX_PATH may be larger than the current operating-system version will handle. Example /* MAKEPATH.C */ #include <stdlib.h> #include <stdio.h> void main( void ) { char path_buffer[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" ); printf( "Path created with _makepath: %s\n\n", path_buffer ); _splitpath( path_buffer, drive, dir, fname, ext ); printf( "Path extracted with _splitpath:\n" ); printf( " Drive: %s\n", drive ); printf( " Dir: %s\n", dir ); printf( " Filename: %s\n", fname ); printf( " Ext: %s\n", ext ); } Output Path created with _makepath: c:\sample\crt\makepath.c Path extracted with _splitpath: Drive: c: Dir: \sample\crt\ Filename: makepath Ext: .c File Handling Routines See Also _fullpath, _splitpath _splitpath, _wsplitpath Break a path name into components. void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext ); void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext ); Routine Required Header Compatibility _splitpath <stdlib.h> Win 95, Win NT _wsplitpath <stdlib.h> or <wchar.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters path Full path drive Optional drive letter, followed by a colon (:) dir Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used. fname Base filename (no extension) ext Optional filename extension, including leading period (.) Remarks The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tsplitpath _splitpath _splitpath _wsplitpath Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don’t need. Example /* MAKEPATH.C */ #include <stdlib.h> #include <stdio.h> void main( void ) { char path_buffer[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" ); printf( "Path created with _makepath: %s\n\n", path_buffer ); _splitpath( path_buffer, drive, dir, fname, ext ); printf( "Path extracted with _splitpath:\n" ); printf( " Drive: %s\n", drive ); printf( " Dir: %s\n", dir ); printf( " Filename: %s\n", fname ); printf( " Ext: %s\n", ext ); } Output Path created with _makepath: c:\sample\crt\makepath.c Path extracted with _splitpath: Drive: c: Dir: \sample\crt\ Filename: makepath Ext: .c File Handling Routines See Also _fullpath, _getmbcp, _makepath, _setmbcp
mistletoejack 2017-11-14
  • 打赏
  • 举报
回复
*(strrchr(szPath, '\\') + 1) = 0;
schlafenhamster 2017-11-13
  • 打赏
  • 举报
回复
那个path应该是你 find 时 要保存的。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧