C/C++ 文件路径解析
2016-03-02 19:14
525 查看
本系列文章由 @yhl_leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/50782054
首先,看一下C/C++中的路径解析与合成的两个函数
不要被复杂的函数声明给吓到,其实很简单。
对于
对于
给出一段代码示例:
其中的一些宏定义如下:
有时候,我们仅需要获得文件的文件名或者拓展名,那么用上述的方法就有点繁琐,
同理,如果想用该方法获取文件名,只需将
文章链接: http://blog.csdn.net/yhl_leo/article/details/50782054
首先,看一下C/C++中的路径解析与合成的两个函数
_splitpath与
_makepath:
_CRT_INSECURE_DEPRECATE(_splitpath_s) _CRTIMP void __cdecl _splitpath(_In_z_ const char * _FullPath, _Pre_maybenull_ _Post_z_ char * _Drive, _Pre_maybenull_ _Post_z_ char * _Dir, _Pre_maybenull_ _Post_z_ char * _Filename, _Pre_maybenull_ _Post_z_ char * _Ext); void __cdecl _makepath(char* _Path, const char* _Drive, const char* _Dir, const char* _Filename, const char* _Ext);
不要被复杂的函数声明给吓到,其实很简单。
对于
_splitpath路径解析函数:
_FullPath: 全路径(
input)
_Drive: 盘符(
output)
_Dir: 除去盘符和文件名的中间路径(
output)
_Filename:文件名(
output)
_Ext:拓展名(
output)
对于
_makepath路径合成函数,上述的参数含义相同,只是输入与输出相反。
给出一段代码示例:
#include <stdlib.h> #include <stdio.h> void main( void ) { char full_path[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _makepath( full_path, "c", "\\sample\\file\\", "makepath", "c" ); printf( "_FullPath created with _makepath: %s\n\n", full_path); _splitpath( full_path, 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 _FullPath created with _makepath: c:\sample\file\makepath.c Path extracted with _splitpath: _Drive: c: _Dir: \sample\file\ _Filename: makepath _Ext: .c
其中的一些宏定义如下:
/* * Sizes for buffers used by the _makepath() and _splitpath() functions. * note that the sizes include space for 0-terminator */ #define _MAX_PATH 260 /* max. length of full pathname */ #define _MAX_DRIVE 3 /* max. length of drive component */ #define _MAX_DIR 256 /* max. length of path component */ #define _MAX_FNAME 256 /* max. length of file name component */ #define _MAX_EXT 256 /* max. length of extension component */
有时候,我们仅需要获得文件的文件名或者拓展名,那么用上述的方法就有点繁琐,
string类型及其操作函数就能很方便地实现:
string filePath = "E:\\file\\main.cpp"; string extendName; int iBeginIndex = filePath.find_last_of(".")+1; int iEndIndex = filePath.length(); extendName = filePath.substr( iBeginIndex, iEndIndex-iBeginIndex ); transform( extendName.begin(), extendName.end(), extendName.begin(), tolower ); cout << extendName << endl; // Output : cpp
同理,如果想用该方法获取文件名,只需将
filePath.find_last_of(".")修改为
filePath.find_last_of("\\"),另外,这里为了使输出的文件拓展名一致,使用了
transform函数,其中的参数
tolower,含义是如果该字符有对应的小写字符,则转为小写字符。
相关文章推荐
- C/C++ 文件路径解析
- C语言数组
- C++第一次上机实验
- 最小对/优先队列(C语言实现)
- 《C++ Primer》之指向函数的指针
- 关于C++内存中字节对齐问题的详细介绍
- C++在字符串前加一个L作用:
- c语言之堆,栈,队列
- Defaulted 和 Deleted 函数--C++11
- Lambdas(C++11新增)
- C语言 简单的队列(数组队列)
- c语言:Hanoi(汉诺)塔问题,求移动盘子的步骤
- Boost 学习之Align-序言部分
- 【C++】实现动态顺序表的PushBack(),PopBack(),PushFront(),PopFront(),Find(),Insert
- C++学生信息处理
- 【C++函数重载】求3个数中最大的数…
- 【c++】用c++编写的求任意区间的素…
- 构造函数
- #include<bits/stdc++.h>
- C++中ShallowCopy和DeepCopy的区别