1

I've got the following text file

MTP[CATALOGUE] 1 false
MTP[SONGS] 3
1
Imagine
John Lennon
1971 233 pop sad YkgkThdzX-8 9
2
Hotel California
Eagles
1977 411 rock calm x47aiMa1XUA 5
3
Another Brick In The Wall
Pink Floyd
1979 360 rock energetic YR5ApYxkU-U 12

I have a Song class, and a vector of Song to store each one. Basically I want it to read the file, and when it arrives to the index, start reading the songs. The first line is the id, the second the title, the third the artist, and the last one is the year, duration, etc.

I've tried the following approach, but It didn't work as expected:

void Catalogo::leerCatalogo() {
    ifstream file(myPath);
    if (file.is_open()) {
        string mark, youtube;
        file >> mark >> this->version >> boolalpha >> this->youtube >> noboolalpha;
        file >> mark >> this->size;
        for (int i = 0; i < this->size; i++) {
            int id;
            file >> id;
            string title;
            getline(file, title);
            cout << id << endl;
            cout << title << endl;
            //...

        }
        file.close();
    }
}

With this approach, I don't get anything readable except for the first id. I think it's because of the mixture of getline and >> operator. Any ideas on how to achieve this?

Norhther
  • 792
  • 2
  • 9
  • 24
  • `getline` will stop at the newline. When you got `id`, it left the newline after it. Does that help? – jxh May 04 '20 at 22:19
  • @jxh I read the post and now I get it... Too much python, I havent been playing with C++ for years, but It makes perfect sense – Norhther May 04 '20 at 22:25

0 Answers0