-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfile_test.cpp
68 lines (48 loc) · 1.55 KB
/
readfile_test.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/constants.hpp>
#include<boost/tokenizer.hpp>
void test_1(char *fileName);
void test_2(char* fileName);
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
test_1(argv[1]);
test_2(argv[1]);
}
void test_1(char *fileName) {
std::cout << "==== Using split ====" << std::endl;
std::ifstream file(fileName);
std::string str;
while (std::getline(file, str)) {
std::cout << "For " << str << " = ";
std::vector<std::string> splitted;
boost::split (splitted, str, boost::is_any_of("##"), boost::token_compress_on);
std::cout << "[";
for (std::vector<std::string>::iterator it = splitted.begin(); it != splitted.end(); ++it) {
std::cout << "\"" << (*it) << "\",";
}
std::cout << "]; Size: " << splitted.size() << std::endl;
}
}
void test_2(char* fileName) {
std::cout << "==== Using tokenizer ====" << std::endl;
std::ifstream file(fileName);
std::string str;
while (std::getline(file, str)) {
std::cout << "For " << str << " = ";
boost::char_separator<char> sep("##");
boost::tokenizer<boost::char_separator<char> > tok(str, sep);
std::cout << "[";
for(boost::tokenizer<boost::char_separator<char> >::iterator it=tok.begin(); it!=tok.end();++it){
std::cout << "\"" << (*it) << "\",";
}
std::cout << "]; " << std::endl;
}
}