WinAPI 文件输入/输出使用 std::strings 而不是 char 数组?
由于性能原因,我不想只使用一次 fstream。使用带有 std::string 的 WinAPI 函数而不是普通的 char 数组似乎是一个非常糟糕的主意。总而言之,我希望您告诉我为什么以下代码片段不起作用(空 stBuffer 保持为空)以及我需要做什么才能修复它。
提前致谢!
std::size_t Get(const std::string &stFileName, std::string &stBuffer)
{
HANDLE hFile = ::CreateFileA(stFileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesRead = 0;
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize = ::GetFileSize(hFile, NULL);
stBuffer.reserve(dwFileSize + 1);
::ReadFile(hFile, &stBuffer[0], dwFileSize, &dwBytesRead, NULL);
stBuffer[dwFileSize] = '\0';
::CloseHandle(hFile);
}
return dwBytesRead;
}
due to performance reasons I didn't feel like using fstream for just one time. Seems like a very bad idea to use WinAPI functions with a std::string instead of a plain char array. All in all I would like you to tell me why the following snippet just won't work (empty stBuffer stays empty) and what I'd need to do to get it fixed.
Thanks in advance!
std::size_t Get(const std::string &stFileName, std::string &stBuffer)
{
HANDLE hFile = ::CreateFileA(stFileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesRead = 0;
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize = ::GetFileSize(hFile, NULL);
stBuffer.reserve(dwFileSize + 1);
::ReadFile(hFile, &stBuffer[0], dwFileSize, &dwBytesRead, NULL);
stBuffer[dwFileSize] = '\0';
::CloseHandle(hFile);
}
return dwBytesRead;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
std::string
可以包含嵌入的'\0'
字符,因此它必须以单独的方式跟踪其自身的长度。您的问题是
std::string::reserve()
不会更改字符串的长度。它只是预先分配一些内存供字符串增长。解决方案是使用 std::string::resize() 并让 WinAPI 函数覆盖字符串内容。旁注:目前,不能保证
std::string
使用连续的缓冲区,但据我所知,所有当前的实现都使用连续的缓冲区,并且这将是下一个标准。Because a
std::string
can contain embedded'\0'
characters, it has to keep track of its own length in a separate way.Your problem is that
std::string::reserve()
does not change the length of the string. It just pre-allocates some memory for the string to grow into. The solution is to usestd::string::resize()
and let the WinAPI function overwrite the string contents.As a side-note: Currently, it is not guaranteed that
std::string
uses a contiguous buffer, but to my knowledge, all current implementations do use a contiguous buffer and it will be a requirement in the next standard.考虑
reserve()
和resize()
成员之间的差异。所以解决方案是:Consider difference between
reserve()
andresize()
members. So the solution would be: