-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
28 lines (26 loc) · 833 Bytes
/
split.cpp
File metadata and controls
28 lines (26 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "split.h"
#include <fstream>
#include <cstdio>
size_t split_to_temp_files(const std::string& file_name, const size_t mem_limit) {
size_t files_written = 0;
size_t mem_used = 0;
std::ifstream input(file_name);
std::ofstream output(std::to_string(files_written));
for (std::string line; std::getline(input, line);) {
if (mem_used + line.size() + 1 > mem_limit) {
++files_written;
output.close();
output.clear();
output.open(std::to_string(files_written));
mem_used = 0;
}
output << line << '\n';
mem_used += line.size() + 1;
}
return ++files_written;
}
void clear_temp_files(const size_t file_count) {
for (size_t i = 0; i < file_count; ++i) {
std::remove(std::to_string(i).c_str());
}
}