C++ – 使用C++标准库过滤Windows文件名中的非法字符
原创文章,作者:StubbornHuang,如若转载,请注明出处:《C++ – 使用C++标准库过滤Windows文件名中的非法字符》https://www.stubbornhuang.com/632/
1 使用C++标准库过滤Windows文件名中的非法字符
在windows系统上有一些字符识别是不能存在于文件名之中的,不然会导致创建文件失败,所以写了一个过滤函数过滤文件名中的非法字符:
代码示例:
template <typename T>
bool MatchInvalidCharPredicate(const T& t)
{
unsigned char t1 = (unsigned char)t;
if (t1 <= 0x1F
|| t1 == 0x7F
|| t1 == 0x5C // \
|| t1 == 0x2F // /
|| t1 == 0x3A // :
|| t1 == 0x2A // *
|| t1 == 0x3F // ?
|| t1 == 0x22 // "
|| t1 == 0x3C // <
|| t1 == 0x3E // >
|| t1 == 0x7C // |
)
{
return true;
}
return false;
}
template<typename C, class T>
void FilterInvalidFileNameChar(T& c)
{
std::replace_if(c.begin(), c.end(), MatchInvalidCharPredicate<C>, L'_');
}
void TestFilterInvalidFileNameChar()
{
std::wstring wss(L"/as中国fasdfas?asdfas*dfa.txt");
FilterInvalidFileNameChar<wchar_t>(wss);
std::cout << "======TestFilterInvalidFileNameChar=================" << std::endl;
std::wcout << Unicode2Ansi(wss.c_str()) << std::endl;
}
当前分类随机文章推荐
- C++ 11 - final关键字简要介绍
- C++ - vector存储动态指针时正确释放内存
- C++11/std::thread - 线程管理join/detach
- C++11 - override关键字简要介绍
- C++ - 获取当前进程内存使用情况
- C++ - 得到字符串中某个字符串出现的个数
- C++11 - 父类与子类相互包含的时候该如何正确的使用智能指针,防止循环引用
- C++11 - 基于无锁队列的单生产者单消费者模型
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志
- C++ - int转string方法总结
全站随机文章推荐
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法
- Modern OpenGL - GLSL着色语言3:GLSL中的数据类型
- C++ - queue存储动态指针时正确释放内存
- failed to find an available destination > EOF
- WordPress - 应对暴力破解登录和规避DDOS的几种方法
- WordPress - 在浏览器的console输出信息
- C++11/std::atomic - 原子变量(不加锁实现线程互斥)
- Duilib - pos和padding属性设置的顺序
- C++ - vector存储动态指针时正确释放内存
- 左右手坐标系与旋转正向