cplusplus文件管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;
std::string path = "/Users/oliverzhu/Desktop/test_video/";
for (const auto &entry : fs::directory_iterator(path)){
        //将文件夹下所有文件名得到
std::string inputpath = entry.path();
std::string::size_type nPos1 = std::string::npos;
std::string::size_type nPos2 = std::string::npos;
nPos1 = inputpath.find_last_of("/");
nPos2 = inputpath.find_last_of(".");
        //截取/开始 长度为nPos2-nPos1-1的字符串
std::string strPath = inputpath.substr(nPos1 + 1, nPos2-nPos1-1);
std::string input_path;
std::string output_path;
if (nPos2-nPos1 > 1)
{
// vaild input path
input_path = inputpath;
output_path = "/Users/oliverzhu/Desktop/results/" + strPath + ".mp4";
std::cout << input_path << std::endl;
std::cout << output_path << std::endl;
        }

https://codeantenna.com/a/vhWRDy0wan