C/C++获取文件的文件名跟扩展名

C/C++获取文件的文件名和扩展名
想要一个不带路径的文件获取文件名和扩展名,并且文件名不带扩展名,扩展名不带“.”
------解决思路----------------------
找到最后一个.的位置拆一下不就可以么。
------解决思路----------------------
可以利用CString类  非MFC环境可以加<atlstr.h> 用CString::ReverseFind找最后的.的位置   CString Mid Left Right利用获得的位置获取.前后的字串就是需要的文件名和扩展名
------解决思路----------------------
_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

------解决思路----------------------
std::string最简单
PS:学编程,不要想别人喂饭,应该自己主动学习
------解决思路----------------------
使用预定义宏,完整体C代码:C/C++获取文件的文件名跟扩展名
------解决思路----------------------
参见博文:http://blog.****.net/hong__fang/article/details/41448777
------解决思路----------------------
参考:http://www.cnblogs.com/mfryf/archive/2012/04/04/2432016.html

至于一些细小问题,如扩展名不想带“.”,用常规的字符串操作就可以了

要看你用的是哪种字符串,是char[]、std::string或者其他

不过不管什么,实现起来都不难

建议楼主自己尝试一下,可以学到很多