mardi 2 juin 2020

Read file with separation in C++

I have a script which reads text file into a vector.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

struct Items
{
    string Name;
    int Number1;
    double Number2;
};

int main()
{
    ifstream file("file4.txt"); 
    vector<Items> player_data;
    Items player_info;
    while (file >> player_info.Name >> player_info.Number1 >> player_info.Number2) { 
        player_data.push_back(player_info);
    }

    for (auto &i : player_data) {
        std::cout << i.Name << ", " << i.Number1 << ", " << i.Number2 << endl;
    }
}

The file4.txt contains as follows:

Wogger Wabbit
2
6.2
Bilbo Baggins
111
81.3
Mary Poppins
29
154.8

How can I display the data properly? How could I give the separation which is a line break ('\n')?

Aucun commentaire:

Enregistrer un commentaire