-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
74 lines (53 loc) · 1.67 KB
/
main.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
int32_t file_number;
std::string output_file_name;
std::string file_name;
std::string getNextOutput_fileName();
int main(int argc, char ** argv) {
std::string first_line;
std::string buffer_line;
int32_t current_line;
int32_t max_lines;
if(argc==1){
perror("to be used as ");
perror(argv[0]);
perror(" file_name \n");
exit(1);
}
if(argc==2){
file_name = std::string(argv[1]);
max_lines = 1000000;
}
std::ifstream input_file(file_name);
std::getline(input_file, first_line);
first_line += "\n";
file_name = file_name.replace(file_name.end()-4,file_name.end(),"");
file_number = 0;
std::ofstream output_file(getNextOutput_fileName().c_str(), std::ofstream::trunc);
output_file.write(first_line.c_str(),first_line.length());
current_line = 0;
while(!input_file.eof()) {
current_line++;
std::getline(input_file, buffer_line);
buffer_line += "\n";
output_file.write(buffer_line.c_str(),buffer_line.length());
if(current_line>max_lines){
output_file.close();
current_line = 0;
output_file = std::ofstream(getNextOutput_fileName());
output_file.write(first_line.c_str(),first_line.length());
}
}
if(output_file.is_open())
output_file.close();
return 0;
}
std::string getNextOutput_fileName(){
file_number++;
output_file_name = file_name+"_";
output_file_name += std::to_string(file_number);
output_file_name += ".csv";
std::cout<<"OutputFile : "<<output_file_name<<std::endl;
return output_file_name;
}