为什么在Windows上std::fopen和std::filebuf::open会裁剪尾随空格字符
Why does std::fopen and std::filebuf::open trim trailing space characters on Windows?
在尝试将一段c++代码从MSWindows移植到linux (Mint10)时,我偶然发现了一个奇怪的问题。我发现在硬编码的文件名中有一个小错别字:末尾有一个空格字符(x20)。
代码在Windows (Windows Vista/7)下工作得很好,但在linux下处理错误的文件名时遇到了麻烦。我发现std::filebuf::open
和std::fopen
(以及boost::filesystem::filebuf
)都允许在Windows下的文件名中尾随空格字符(我认为这是一种不正确的行为)。
我在Win7下使用MSVC2010和MinGW测试了以下代码:
# include <iostream>
# include <fstream>
# include <string>
# include <cstdlib>
const std::string filename = "Z:\Path\To\A\File " ; // one trailing space
int main(int, char * [])
{
std::cout << "Testing with file : "" << filename << """ << std::endl ;
// Testing with std::filebuf::open
std::filebuf fb ;
fb.open(filename.c_str(), std::ios::in | std::ios::binary) ;
if( fb.is_open() )
{
std::cout << "Using std::filebuf : Opened" << std::endl ;
fb.close() ;
}
else
{
std::cout << "Using std::filebuf : Not opened" << std::endl ;
}
// Testing with std::fopen
std::FILE * fp = std::fopen(filename.c_str(), "rb") ;
if( fp )
{
std::cout << "Using std::fopen : Opened" << std::endl ;
std::fclose(fp) ;
}
else
{
std::cout << "Using std::fopen : Not opened" << std::endl ;
}
return 0 ;
}
我确认硬盘上的文件名称中没有尾随空格字符。该文件在Windows上成功打开,但如果文件名不完全匹配,则在linux下无法访问。
我有几个问题:
- 这是Windows下文件API的正常行为吗?还有其他陷阱吗?我在谷歌上搜索了一下,但没有找到有关此案的资料。(也许我是一个糟糕的谷歌人:))
- 如何写一个代码,工作在一个严格的时尚与文件名?在将文件名传递给某些文件函数之前总是修剪文件名并不是我这种情况的解决方案;我目前的应用程序必须列出目录(使用
boost::filesystem::directory_iterator
)和修剪文件名将打破应用程序,如果用户提供的文件名与尾部空格字符(这是罕见的,但允许在许多文件系统)。
谢谢你的建议
Windows本身会从文件名中去掉后面的空格,这与您用来打开文件的c++函数无关。
在Windows API函数CreateFile: INFO:以空格或句号结尾的文件名不受支持
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 在Windows上用C++裁剪HBITMAP
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 为什么在Windows上std::fopen和std::filebuf::open会裁剪尾随空格字符