C++文件系统
C++ File System
我正在尝试添加这些功能。
std::string GetDirectory(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
path_name.find_last_of(DIRECTORY_SEPARATOR),
path_name.length()
)
);
return path_name;
}
std::string GetName(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
0,
path_name.find_last_of(DIRECTORY_SEPARATOR)
)
);
path_name.erase(
path_name.substr(
path_name.find_last_of("."),
path_name.length()
)
);
return path_name;
}
std::string GetExtension(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
0,
path_name.find_last_of('.')
)
);
return path_name;
}
我正在尝试添加:
C:\Windows\Users\Example\Desktop\test.txt
获取目录
C:\Windows\Users\Example\Desktop
GetName
test
获取扩展
txt
我在 path_name 和擦除(句号)之间遇到错误。
帮助将不胜感激,谢谢。
请请求错误消息:
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(368,5): warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory.
1> main.cpp
1>main.cpp(287): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int'
1> main.cpp(287): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> main.cpp(301): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int'
1> main.cpp(301): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> main.cpp(307): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int'
1> main.cpp(307): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> main.cpp(321): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int'
1> main.cpp(321): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
- 删除无用和有害的
path_name.substr(
和)
。
- 您可以省略
std::string::erase
的第二个参数以擦除直到字符串结束。
你应该使用
template <class Path> typename Path::string_type extension(const Path& p);
获取扩展名的函数。
你应该使用
template <class Path> typename Path::string_type basename(const Path& p);
获取文件基本名称的函数。
我假设您使用的是 Windows OS.
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
namespace fs = boost::filesystem;
std::string p = "C:\Windows\Users\Example\Desktop\test.txt";
std::string ext = fs::extension(p);
std::string bas = fs::basename(p);
std::cout << ext << " " << bas << std::endl;
return 0;
}
我正在尝试添加这些功能。
std::string GetDirectory(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
path_name.find_last_of(DIRECTORY_SEPARATOR),
path_name.length()
)
);
return path_name;
}
std::string GetName(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
0,
path_name.find_last_of(DIRECTORY_SEPARATOR)
)
);
path_name.erase(
path_name.substr(
path_name.find_last_of("."),
path_name.length()
)
);
return path_name;
}
std::string GetExtension(std::string path_name) {
boost::filesystem::path path(path_name.c_str());
path_name = path.string();
path_name.erase(
path_name.substr(
0,
path_name.find_last_of('.')
)
);
return path_name;
}
我正在尝试添加:
C:\Windows\Users\Example\Desktop\test.txt
获取目录
C:\Windows\Users\Example\Desktop
GetName
test
获取扩展
txt
我在 path_name 和擦除(句号)之间遇到错误。 帮助将不胜感激,谢谢。
请请求错误消息:
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(368,5): warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory. 1> main.cpp 1>main.cpp(287): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int' 1> main.cpp(287): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> main.cpp(301): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int' 1> main.cpp(301): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> main.cpp(307): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int' 1> main.cpp(307): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> main.cpp(321): error C2664: 'std::_String_iterator>> std::basic_string,std::allocator>::erase(std::_String_const_iterator>>,std::_String_const_iterator>>)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'unsigned int' 1> main.cpp(321): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
- 删除无用和有害的
path_name.substr(
和)
。 - 您可以省略
std::string::erase
的第二个参数以擦除直到字符串结束。
你应该使用
template <class Path> typename Path::string_type extension(const Path& p);
获取扩展名的函数。
你应该使用
template <class Path> typename Path::string_type basename(const Path& p);
获取文件基本名称的函数。
我假设您使用的是 Windows OS.
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
namespace fs = boost::filesystem;
std::string p = "C:\Windows\Users\Example\Desktop\test.txt";
std::string ext = fs::extension(p);
std::string bas = fs::basename(p);
std::cout << ext << " " << bas << std::endl;
return 0;
}