Parse .txt and find a word(s) and surrounding data?

Hi all,

Just wondering if someone could provide some sample code to read from a specified .txt file, find a word and then output that word, and, say, any words around it.

This would allow me to parse certain settings files and find out the setting listed under a certain 'title' (Such as... Total Physical Memory: 'xxxxxxx').

Any ideas as to how I could parse a text file for specific word or group of words and then also output data AROUND what was searched for?

Thanks for your help!
You might want to try a different approach. From the example you gave of Total Physical Memory, I'd say you want to parse the file in to keys ("Total Physical Memory") and values ('xxxxxxxx'). When you find the keyword that you're interested in, you can print it out.

If I'm on the wrong track then can you provide an example of an actual input file and what you'd like to output?
You have the right idea, somewhat. :)

I'd be looking for the words 'Total Physical Memory:' and then, by parsing that line, I'd need some code that could detect an 'int' (or a 'long'?) that was formatted as if it was the number representing the Total Physical Memory.

In effect, the code would have to know, based on the words 'Total Physical Memory' that the following numbers should be placed into an int/long for later usage in the program.

I guess it would be something to do with the getLine function but I'm not sure how to specify the specific 'word' (or number, really) that the code would have to extract from that line and place into an int/long.
Here's the code I've got so far (I'm very new to this so possibly totally on the wrong track here...)

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    int LineNumber;
    std::ifstream file ("Example.txt");
    string holderForGetLineData;
    std::string FindThis ("Number");

    while(file.is_open())
    {

        std::getline(file, holderForGetLineData);
        LineNumber++;

        if(FindThis.compare (holderForGetLineData) == 0 )
        {
            break;
        }

    }

    cout << holderForGetLineData << endl;

    system("pause");

    return 0;
}


The problem is, when the output window opens when I compile&run the program nothing happens after the "Hello World" output. It just sits there with a blinking cursor. No errors are reported.

Suggestions?
Unless you have a line in the file that contains only the word "Number", the condition on line 23 will never be true and line 25 will never be reached. Since the file is never closed, the condition on line 17 will never be false if it was ever true.

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


auto as_lower(std::string s) {
    for (auto&ch : s) ch = std::tolower(ch);
    return s;
}

auto find_occurrences(const std::string& file_name, std::string pattern) {
    pattern = as_lower(std::move(pattern));

    auto lines = std::vector<std::string>{};
    auto line = std::string{};
    auto in = std::ifstream{file_name};

    while (getline(in, line))
        if (as_lower(line).find(pattern) != std::string::npos)
            lines.push_back(line);

    return lines;
}

int main()
{
    auto lines = find_occurrences("Example.txt", "Number");

    if (lines.size()) {
        std::cout << lines.size() << " occurrences were found:\n";
        for (const auto& line : lines) 
            std::cout << "  " << std::quoted(line) << '\n' ;
    } else {
        std::cout << "No lines found.\n";
    }
}
Topic archived. No new replies allowed.