c++ - 当 ifstream 读取到大于 ~1MiB 的缓冲区时,为什么程序会崩溃?
问题描述
我正在使用以下代码:
const size_t MonaBuffSize = 1024 * 1000;
std::ifstream file(Path.string(), std::ifstream::binary);
MD5_CTX md5Context;
MD5_Init(&md5Context);
char buf[MonaBuffSize];
while (file.good()) {
file.read(buf, sizeof(buf));
MD5_Update(&md5Context, buf, file.gcount());
}
unsigned char result[MD5_DIGEST_LENGTH];
MD5_Final(result, &md5Context);
当我将 MonaBuffSize 设置为 1024 * 1024 - 调用 file.read 函数时程序崩溃。
ifstream.read() 函数中是否有任何缓冲区大小限制?我知道使用较小的缓冲区可能会更好,但我想了解这里有什么问题。
我正在使用 Visual Studio 2019,C++ 17。
编辑:按照评论中的建议,我回到了与 boost::iostreams::mapped_file_source 一起使用的上一个方法:
const std::string md5_from_file(const std::filesystem::path Path, std::uintmax_t Size)
{
unsigned char result[MD5_DIGEST_LENGTH];
if (Size > 0) {
try {
boost::iostreams::mapped_file_source src;
src.open(Path.string());
MD5((unsigned char*)src.data(), src.size(), result);
}
catch (std::ios_base::failure const& e)
{
MD5((unsigned char*)"", 0, result);
//MessageBox(NULL, e.what(), "EXCEPTION", MB_YESNO | MB_ICONINFORMATION);
}
}
else {
MD5((unsigned char*)"", 0, result);
}
std::ostringstream sout;
sout << std::hex << std::setfill('0');
for (auto c : result) sout << std::setw(2) << (int)c;
return sout.str();
}
此外,我意识到是 Windows Defender 大大降低了磁盘读取速度,所以我试图加快它的速度是毫无意义的。
解决方案
推荐阅读
- vue.js - filter query param not working through axios get from my vue js application?
- highcharts - Highchart yAxis.ceiling 无法正常工作
- python - 使用 Numpy 时收到带有满秩矩阵的错误消息“LinAlgError:奇异矩阵”
- python - 如何取消pyspark foreachPartition操作
- kubernetes - Terraform - Kubernetes - Helm 发布 - 生成密码
- node.js - 当人们说 KOA2 时,他们到底在说什么?
- python - 我可以使用 Popen 以交互方式将命令传递和读取到被调用的进程吗?
- curl - 为不存在的地址 mitmproxy 拦截 https 的问题
- python - 抽象模型中的 django datetimefield 无法更新“auto_now_add”、“auto_now”
- c++ - 为什么是0;C++ 中的有效语句?