获取文件名和路径c++代码
时间: 2023-11-27 11:54:25 浏览: 39
获取文件名和路径的C++代码如下:
```c++
#include <iostream>
#include <string>
#include <filesystem>
int main()
{
std::string filepath = "/home/user/example.txt";
std::filesystem::path path(filepath);
std::cout << "File name: " << path.filename() << std::endl;
std::cout << "Directory path: " << path.parent_path() << std::endl;
return 0;
}
```
在上面的例子中,我们使用了C++17中的`std::filesystem`库来获取文件路径和文件名。首先,我们将文件路径存储在一个字符串变量`filepath`中。然后,我们将其转换为`std::filesystem::path`类型的对象`path`。最后,我们使用`path.filename()`来获取文件名,使用`path.parent_path()`来获取文件所在的目录路径。
需要注意的是,`std::filesystem`库可能需要在编译时链接额外的库,如`-lstdc++fs`。
相关问题
获取文件名和路径c++代码 vs2015
获取文件名和路径可以通过使用C++标准库中的文件流和字符串处理函数来完成。以下是示例代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
int main()
{
// 打开文件
std::ifstream file("example.txt");
// 获取文件路径
std::string path = "";
char buf[256];
if (GetModuleFileName(NULL, buf, 256) != 0)
{
path = buf;
size_t pos = path.find_last_of("\\/");
path = path.substr(0, pos + 1);
}
// 获取文件名
std::string filename = "example.txt";
size_t pos = filename.find_last_of("\\/");
if (pos != std::string::npos)
{
filename = filename.substr(pos + 1);
}
// 输出结果
std::cout << "Path: " << path << std::endl;
std::cout << "Filename: " << filename << std::endl;
return 0;
}
```
这里使用了`GetModuleFileName`函数来获取当前程序的路径,然后使用字符串处理函数`find_last_of`和`substr`来提取出文件路径和文件名。
获取文件名工具类c++代码
以下是一个获取文件名的工具类的 C++ 代码示例,其中包含了两种方法:
```c++
#include <iostream>
#include <string>
#include <algorithm>
class FilenameUtils {
public:
// 方法 1:使用 std::string::find_last_of() 和 substr() 函数
static std::string getFileName(const std::string& path) {
size_t found = path.find_last_of("/\\");
return path.substr(found + 1);
}
// 方法 2:使用 std::string::find_last_of() 和 std::string::erase() 函数
static std::string getFileName2(const std::string& path) {
size_t found = path.find_last_of("/\\");
if (found != std::string::npos) {
return path.erase(0, found + 1);
}
return "";
}
};
int main() {
std::string path = "/path/to/file.txt";
std::string filename = FilenameUtils::getFileName(path);
std::cout << "Filename: " << filename << std::endl;
std::string path2 = "C:\\path\\to\\file.txt";
std::string filename2 = FilenameUtils::getFileName2(path2);
std::cout << "Filename: " << filename2 << std::endl;
return 0;
}
```
这个工具类包含了两种方法:
- 方法 1:使用 `std::string::find_last_of()` 和 `substr()` 函数来获取最后一个路径分隔符后面的子字符串,即文件名。
- 方法 2:使用 `std::string::find_last_of()` 和 `std::string::erase()` 函数,先删除路径分隔符及其前面的字符串,再返回剩余的字符串作为文件名。
你可以根据自己的需求选用其中的一种方法实现文件名的获取。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)