forked from softwarecomputationalbiologyufpa/FAIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.cpp
53 lines (43 loc) · 1.14 KB
/
cmd.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
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include <fstream>
using namespace std;
int exec(const char* cmd) {
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return atoi(result.c_str());
}
int main (int argc, char *const argv[])
{
string filepath = argv[1];
string command = "wc -l " + filepath;
ifstream fin;
fin.open(filepath);
if (fin.is_open())
{
cout << "File open." << endl;
}
int number_of_lines = exec(command.c_str());
cout << "Number of lines:" << number_of_lines << endl;
int threads = 2;
int lines_by_thread = number_of_lines / threads;
fin.seekg();
string lines[lines_by_thread];
for (int i = 0; i < lines_by_thread; i++)
if (!getline(fin, lines[i]))
cout << "EOF" << endl;
else
cout << lines[i] << endl;
return 0;
}