-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractData.cpp
More file actions
88 lines (81 loc) · 3.01 KB
/
extractData.cpp
File metadata and controls
88 lines (81 loc) · 3.01 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "stdio.h" // printf
#include <iostream> // std::cout
#include <iomanip> // exit()
#include <fstream> // file handling
#include <string> // std::string
#include <vector> // std::vector<>
std::vector<std::string> handelLine(std::string myLine){
// This function takes a string as the input.
// The string may contain spaces (i.e., ' ').
// The function segments the string into several smalle strings
// based on the spaces.
// The output is a vector of strings.
std::vector<std::string> sample(1); // creat a vector of strings, len = 1
int numSeg = 0;
int idx = 0;
for (int i=0; i < myLine.size(); i++){
if (myLine[i] != ' '){ // the current segment is not completed
sample[numSeg] += myLine[i]; // add the char to the string
}
else{
numSeg ++; // current segment completed, move to the next segment
sample.resize(numSeg + 1); // increase the vector size by 1
}
}
return sample;
}
std::vector<std::vector<std::string>> handleData(std::string fileName){
// This function takes the name of a file (e.g., 'data.txt') as the input.
// It reads the file line by line.
// Each line is a string with spaces (i.e., ' ').
// The function segments each line into several strings based on the spaces.
// The output is a 2D matrix. Each row carries the segmented strings
// of one line.
std::ifstream myFile; // create an object to read the data txt.
myFile.open(fileName);
if (!myFile){
std::cout << "Unable to open file" << std::endl;
exit(1);
}
std::string myLine; // create an object to read one line of the data
int numLine = 0;
// create a 2D matrix with strings as entries
// each row of this matrix contains all the segments of a line
std::vector<std::vector<std::string>> samples;
while (!myFile.eof()){ // while not reaching the end of the file
if (std::getline(myFile, myLine)){ // try read a line
samples.resize(numLine + 1);
samples[numLine] = handelLine(myLine);
numLine ++;
}
}
return samples;
}
int main(){
std::string fileName = "data.txt";
std::ofstream myFile ("srcIPs.txt");
std::string value (10, '*'); // the value of the key, set as 10 "*"s.
std::vector<std::vector<std::string>> samples = handleData(fileName);
int numSamples = samples.size();
int maxSize = 0;
for (int i = 0; i < numSamples; i++){ // Find the largest IP length
if (samples[i][1].size() > maxSize){
maxSize = samples[i][1].size();
}
}
for (int i = 0; i < samples.size(); i ++){
myFile << samples[i][1]; // the srcIP
int diff = maxSize - samples[i % samples.size()][1].size();
if (diff > 0){
for (int j = 0; j < diff; j++){
myFile << ".";
}
}
myFile << value;
// myFile << std::endl;
}
}
// to make:
// g++ -std=c++11 extractData.cpp -o test
// to run:
// ./test