Read a txt file and store the content in lists

-1

I need to read a large ".txt" file and its contents are in the following format:

1   12
1   13
2   5
2   6

I would like to know if you can read the value of each column and store it in a list, for example:

Ler "1"
Armazenar o "1" numa lista
Ler "12"
Armazenar 12 numa lista
ler "1"
Armazenar "1" na lista
ler "13"
Armazenar 13 na lista
...

And so on

    
asked by anonymous 12.04.2018 / 17:41

1 answer

2

To have 2 lists, each containing the numbers of a column the code below can help.

int main() {
    std::ifstream file{"text.txt"};

    std::vector<std::string> columnA;
    std::vector<std::string> columnB;
    std::string string;

    int i = 0;
    while ( !file.eof() ) {
        file >> string;
        if (i++ % 2 == 0) columnA.push_back(string);
        else columnB.push_back(string);
    }

    for (auto s : columnA) std::cout << s << "\n";

    std::cout << "\n";

    for (auto s : columnB) std::cout << s << "\n";

    return 0;
}
    
12.04.2018 / 18:52