How can i read only certain info from txt files?

Im wondering how i can make it so i only read certain info from a txt file, for example "Name=Joe" i only want to read Joe. Although Joe can be anything really since im trying to make a user config file.
The equals sign is a delimiter, there are many ways to ignore input until it. Perhaps, ignore()?
http://www.cplusplus.com/reference/iostream/istream/ignore/
Typically you'd want to parse the input file so that you can recognise the different types of data. This example specifically looks for "Name". You may want something more general, but this could be a start.

Input:
Name=Fred
Age=25
Name=Joe Bloggs
Address=1234 Chestnut Drive
Phone=01061 2365164

Output:
Fred
Joe Bloggs
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
#include <iostream>
#include <fstream>
#include <string>

    using std::string;
    using std::cout;
    using std::endl;

string getValue(string str, string label);

int main()
{
    char fname[] = "indata.txt";
    ifstream infile(fname);
    if (!infile)
    {
        cout << "Input error: " << fname << endl;
        return 0;
    }

    string line;
    while (getline(infile, line))
    {
        string name = getValue(line, "Name");
        if (name.length())
            cout << name << endl;
    }

    return 0;
}
//-------------------------------------------------------------------
string getValue(string line, string label)
{
    string result = "";
    char delim = '=';                // Split  line at delimiter
    int pos = line.find(delim);
    if (pos <= 0)
        return result;

    string left  = line.substr(0,pos);
    string right = line.substr(pos+1);

    if (left == label)               // Test for required label
        result = right;

    return result;
}
Last edited on
Topic archived. No new replies allowed.