写入多个文件时崩溃
Crash while writing to multiple files
在用Java编程时,经过长时间的休息,我又开始使用C++了。我正在尝试将我的数据处理工作转换为C++。我在尝试同时打开和写入100多个文件时遇到了一个问题(按日期将10GB的文本文件拆分为多个文件)。同样,我只使用C++大约两天了,所以我已经满了。我的代码充满了其他问题,但我已经创建了最简单的片段来显示这个问题。
- 我可以将files_to_open更改为任何数字,而且我没有任何问题
- 如果files_to_write为<=125它运行文件
- 如果files_to_write大于125,它就会崩溃
是什么原因造成的?
#include <map>
#include <sstream>
int main() {
std::map<int, FILE*> files;
int files_to_open = 200;
int files_to_write = 200;
// Open a set of files.
for(int i = 0; i < files_to_open; i++) {
std::ostringstream file_path;
file_path << "E:\tmp\file_" << i << ".txt";
files[i] = fopen(file_path.str().c_str(), "w");
}
// Write data to files.
for(int i = 0; i < files_to_write; i++) {
printf("%dn", i);
fwrite("Some Data", sizeof(char), 9, files[i]);
}
// Close files.
for (auto& file : files) {
fclose(file.second);
}
// End it all.
printf("Press Any Key to Continuen");
getchar();
return 0;
}
我假设fopen在files_to_write大于125时返回NULL
。您的操作系统对每个进程可以打开的文件句柄数量有限制,您可能已经达到了限制。
125是完全合理的,因为您已经有0(stdin)、1(stdout)和2(stderr),所以再加125就是128,这是一个很好的限制。
无论哪种方式,在盲目写入FILE*
之前,都应该检查fopen
的返回值
相关文章:
- Visual Studio在尝试读取resource.txt文件时崩溃
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- Linux 可执行文件通过 dlopen 在emplace_back崩溃打开共享库
- 为什么从文件获取图标时应用程序有时会崩溃?
- 在目标计算机上访问 POSIX 信号灯时出现可执行文件崩溃(SEGV_MAPERR)
- 当我运行MEX文件时,MATLAB崩溃
- 运行简单的.oct文件时Octave崩溃,我的代码出了什么问题?
- 如何从核心转储文件中识别导致崩溃的完整命令
- 简单的文本文件格式化在linux下崩溃,但在Windows中很好
- 用静态版本的QT编译时,文件对话框崩溃
- MEX文件实现特征库伪内函数崩溃
- Visual C 32位整数从文件到8位字符到文件 - 程序在某些整数上崩溃
- C 通过char读取文件字符串的文件char;崩溃
- 循环导致文件崩溃
- 如何在启动过程中崩溃时使用 gdb 调试可执行文件
- 是否可以以编程方式创建迷你转储文件而不会崩溃?
- 故障转储文件是否需要崩溃时正在运行的本地文件
- QT项目在配置文件上崩溃,与发布和调试器一起运行
- 从CSV文件读取时,试图将值插入一个字符串中时崩溃