Extracting specific data from text file

Pages: 12
I am kinda new to this but im pretty sure you would open a output file with something like

1
2
ofstream variablename; 
variablename.open("whatever file name.txt") ;


and replace cout with variablename

hope that helps ]

Last edited on
sorry @Ripb123 i do not follow you. Can you edit that in this code please?

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>

// true if line begins with s, but ignoring spaces
bool is_match(const std::string& line, const std::string& s) {
    size_t i = 0, j = 0;
    for (;;) {
        while (i < line.size() && isspace(line[i]))
            ++i;
        if (i >= line.size())
            break;
        while (j < s.size() && isspace(s[j]))
            ++j;
        if (j >= s.size())
            break;
        if (line[i++] != s[j++])
            return false;
    }
    return j == s.size();
}

int main() {
    std::vector<std::string> titles {
        "Subscriber number",
        "Termination ID",
        "Equipment ID",
        "AGCU module number",
        "Master/Slave type"
    };

    std::ifstream fin("LTS_IMSBR.txt");
    if (!fin) {
        std::cerr << "Cannot open input file.\n";
        return 1;
    }

    std::string line;

    std::cout << "|Subscriber number"
                 "|Termination ID"
                 "|  Equipment ID  "
                 "|AGCU module number"
                 "|Master/Slave type|\n";

    while (std::getline(fin, line)) {

        std::vector<std::string> values(titles.size());
        bool found = false;

        while (std::getline(fin, line)) {
            if (line.substr(0, 3) == "+++")
                break;

            for (size_t i = 0; i < titles.size(); ++i) {
                if (is_match(line, titles[i])) {
                    size_t pos = line.find('=');
                    if (pos < line.size()) {
                        pos = line.find_first_not_of(" ", pos + 1);
                        if (pos < line.size()) {
                            found = true;
                            values[i] = line.substr(pos);
                        }
                    }
                }
            }
        }
        auto sw = std::setw;
        if (found) {
            std::cout << '|'    << sw(11) << values[0] << "      |";
            std::cout << "   "  << sw( 4) << values[1] << "       |";
            std::cout           << sw(16) << values[2] << "|";
            std::cout           << sw( 8) << values[3] << "          |";
            std::cout << "    " << std::left << sw( 6) << values[4]
                                << std::right << "       |\n";
        }
    }
}


Last edited on
To learn about file I/O, look here:

http://www.cplusplus.com/doc/tutorial/files/
Do you have fgrep on your system?
$ cat x
Subscriber number
Termination ID
Equipment ID
AGCU module number
Master/Slave type

$ fgrep -f x input
Equipment ID = 172.22.5.74:2944
Termination ID = A124
Master/Slave type = Master
Subscriber number = 3302552
Equipment ID = 172.22.5.74:2944
AGCU module number = 23
Equipment ID = 172.22.5.65:2944
Termination ID = A290
Master/Slave type = Slave
Subscriber number = 3302553
Equipment ID = 172.22.5.65:2944
AGCU module number = 23
Equipment ID = 172.22.5.73:2944
Termination ID = A174
Master/Slave type = Slave
Subscriber number = 3312552
Equipment ID = 172.22.5.73:2944
AGCU module number = 23

Topic archived. No new replies allowed.
Pages: 12