-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpartitiondata.c
More file actions
61 lines (53 loc) · 1.21 KB
/
partitiondata.c
File metadata and controls
61 lines (53 loc) · 1.21 KB
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
/*********************
g++ **.c -o ** -std=c++11
./partitiondata [filename] [number]
attention: it will make a new file named "data"+"_"
**********************/
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
#include <chrono>
int main(int argc, char **argv) {
if(argc!=3){
std::cout<<"argv error!\n";
return -1;
}
std::string data_files =argv[1];
int number = atoi(argv[2]);
std::ifstream fin(data_files.c_str());
if(!fin){
std::cout<<"file open error!\n";
return -1;
}
std::ofstream fout[number];
for(int i=0;i<number;i++){
std::string s = argv[1];
std::string buffer = s + "_/part" + std::to_string(i+1);
fout[i].open(buffer.c_str());
if(!fout[i]){
std::cout<<"file open error!\n";
return -1;
}
}
std::string buf;
char str0[] = " ";
int count = 0;
while (getline(fin, buf)){
char temp[buf.size()+1];
buf.copy(temp,buf.size());
temp[buf.size()] = '\0';
strtok(temp, str0);//label
char *result = NULL;
result = strtok(NULL, str0);
if(result == NULL)continue;
fout[count%number]<<buf<<std::endl;
count++;
}
std::cout<<"number of data: "<<count<<std::endl;
fin.close();
for(int i = 0;i<number;i++){
fout[i].close();
}
return 0;
}