How to read from a file from a certain point when select from a ListBox

Hello.. I have a list box and selecting an item from it I want to reproduce some text in a edit control reading from a file from a certain location. This means if I have this file.txt e.g:

file.txt

[Fire]
+3 To Fire
+10 To Dex
-5 To light radius

[Cold]
+3 To Cold
+10 To Strength
-2 To mana

[Lightning]
+3 To Lightning
+10 To Vitality
-2 To Life

[Poison]
.
..
...etc


..and in my listBox I have something like this e.g:

myListBox

Fire
Cold
Lightning
Poison
.
..
...etc


using case LBN_SELECHANGE for the listBox and selecting the Cold from the list to read from the file and to output in a edit rdonly this:

EditControl

+3 To Cold
+10 To Strength
-2 To mana


I will appreciate if anyone could help me.. Thank you in advance !
Last edited on
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

auto read_choices()
{
    vector<string> choices;
    ifstream in("choices.txt");
    string line;
    while (getline(in, line))
        if (line[0] == '[')
            choices.push_back(line.substr(1, line.find(']') - 1));
    return choices;
}

void display_choice(string choice)
{
    ifstream in("choices.txt");
    string line;
    while (getline(in, line))
    {
        if (line[0] == '[' && line.substr(1, line.find(']') - 1) == choice)
        {
            while (getline(in, line) && !line.empty())
                cout << line << '\n';
            return;
        }
    }
    cout << "Your choice is not in the list.\n";
}

int main()
{
    auto choice_list = read_choices();

    for (size_t i = 0; i < choice_list.size(); ++i)
        cout << i << ": " << choice_list[i] << '\n';
    cout << ">>> ";
    int choice;
    cin >> choice;

    display_choice(choice_list[choice]);
}

Last edited on
..Aaa right :| I understand the concept, now I will try to reproduce the same thing in C, sorry dutch I'm not that familiar with C++, but hey thanks for the answer, I appreciate it !! :)
Topic archived. No new replies allowed.