我有两个 std::ofstream 文本文件,每个文件都有一百多个,我想将它们连接起来。使用 fstreams 存储数据以创建单个文件通常会出现内存不足错误,因为大小太大。
有什么方法可以比 O(n) 更快地合并它们?
文件 1 (160MB):
0 1 3 5
7 9 11 13
...
...
9187653 9187655 9187657 9187659
文件 2 (120MB):
a b c d e f g h i j
a b c d e f g h j i
a b c d e f g i h j
a b c d e f g i j h
...
...
j i h g f e d c b a
合并(380MB):
0 1 3 5
7 9 11 13
...
...
9187653 9187655 9187657 9187659
a b c d e f g h i j
a b c d e f g h j i
a b c d e f g i h j
a b c d e f g i j h
...
...
j i h g f e d c b a
文件生成:
std::ofstream a_file ( "file1.txt" );
std::ofstream b_file ( "file2.txt" );
while(//whatever){
a_file << num << endl;
}
while(//whatever){
b_file << character << endl;
}
// merge them here, doesn't matter if output is one of them or a new file
a_file.close();
b_file.close();