本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 使用C++标准库过滤Windows文件名中的非法字符
原文链接:https://www.stubbornhuang.com/632/
发布于:2020年01月09日 17:32:04
修改于:2020年01月09日 17:32:04

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++STL容器 - std::map删除指定元素 阅读462次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读280次,点赞0次
- C++ - linux编译C++代码出现error: use of deleted function std::atomic
::atomic(const std::atomic 阅读350次,点赞0次&) - C++ - C++实现Python numpy的矩阵维度转置算法,例如(N,H,W,C)转换为(N,C,H,W) 阅读1441次,点赞0次
- C++ - 得到字符串中某个字符串出现的个数 阅读2520次,点赞1次
- C++ - 动态链接库dll为什么要使用unsigned char作为byte的内部格式 阅读78次,点赞0次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读459次,点赞0次
- C++11 - 使用std::thread,std::shared_future,std::promise并行化/多线程化for循环,提升处理速度 阅读703次,点赞0次
- Centos7 编译C++项目错误解决 : terminate called after throwing an instance of 'std::regex_error' 阅读1795次,点赞0次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读671次,点赞0次
全站随机文章推荐
- 书籍翻译 - Fundamentals of Computer Graphics, Fourth Edition 虎书第四版中文翻译 阅读1793次,点赞4次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读671次,点赞0次
- 资源分享 - Mathematics for Computer Graphics , Second Edition 英文高清PDF下载 阅读592次,点赞0次
- 资源分享 - TCP/IP网络编程(韩 尹圣雨著 金国哲译)PDF下载 阅读3074次,点赞0次
- Python - 使用Python+websockets时报错:AttributeError: module 'websockets' has no attribute 'serve' 阅读981次,点赞0次
- WordPress - wp_registration_url函数详解 阅读320次,点赞0次
- TensorRT - 使用trtexec工具转换模型、运行模型、测试网络性能 阅读1761次,点赞0次
- 资源分享 - GPU Pro 3 - Advanced Rendering Techniques 英文高清PDF下载 阅读1443次,点赞0次
- 资源分享 - Computational Geometry in C, First Edition 英文高清PDF下载 阅读1108次,点赞0次
- 资源分享 - 计算机图形学,Fundamentals of Computer Graphics(Second Edition) 中文版PDF下载 阅读818次,点赞0次
评论
147