C++;读取不带特定分隔符的唱片集播放列表 我正在创建一个C++程序来模拟点唱机,到目前为止一切都很好。然而,我应该从一个文件中读取相册,考虑到播放列表文件必须使用的格式,在从文件中读取相册时,我很难想出一种分离相册的方法

C++;读取不带特定分隔符的唱片集播放列表 我正在创建一个C++程序来模拟点唱机,到目前为止一切都很好。然而,我应该从一个文件中读取相册,考虑到播放列表文件必须使用的格式,在从文件中读取相册时,我很难想出一种分离相册的方法,c++,c++11,C++,C++11,播放列表文件的给定格式如下: Album Number of songs Song title|Artist|Duration in sec 以及实际播放列表文件的示例: Abbey Road 17 Come Together|The Beatles|260 Something|The Beatles|183 Maxwell's Silver Hammer|The Beatles|207 Oh! Darling|The Beatles|206 Octopus's Garden|The Beat

播放列表文件的给定格式如下:

Album
Number of songs
Song title|Artist|Duration in sec
以及实际播放列表文件的示例:

Abbey Road
17
Come Together|The Beatles|260
Something|The Beatles|183
Maxwell's Silver Hammer|The Beatles|207
Oh! Darling|The Beatles|206
Octopus's Garden|The Beatles|171
I Want You (She's So Heavy)|The Beatles|467
Here Comes the Sun|The Beatles|185
Because|The Beatles|165
You Never Give Me Your Money|The Beatles|242
Sun King|The Beatles|146
Mean Mr. Mustard|The Beatles|66
Polythene Pam|The Beatles|72
She Came in Through the Bathroom Window|The Beatles|117
Golden Slumbers|The Beatles|91
Carry That Weight|The Beatles|96
The End|The Beatles|139
Her Majesty|The Beatles|23
Rubber Soul
14
Drive My Car|The Beatles|150
Norwegian Wood|The Beatles|125
You Won't See Me|The Beatles|202
Nowhere Man|The Beatles|164
Think For Yourself|The Beatles|138
The Word|The Beatles|161
Michelle|The Beatles|160
What Goes On|The Beatles|170
Girl|The Beatles|153
I'm Looking Through You|The Beatles|147
In My Life|The Beatles|148
Wait|The Beatles|136
If I Needed Someone|The Beatles|143
Run For Your Life|The Beatles|138
我有一个名为Song的课程,相关部分如下:

class Song
{
    private:
        std::string title;
        std::string artist;
        Time duration;
        ...
};
class Album
{
    private:
        std::string album_title;
        int numsongs;
        std::vector<Song> songvec;
        ...
};
以及一个名为Album的类,其相关部分如下:

class Song
{
    private:
        std::string title;
        std::string artist;
        Time duration;
        ...
};
class Album
{
    private:
        std::string album_title;
        int numsongs;
        std::vector<Song> songvec;
        ...
};
相册的重载>>运算符:

istream &operator>>(istream &is, Album &album)
{
    Song s;
    string str;

    getline(is, str);
    album.setAlbum(str);

    getline(is, str);
    int num_songs = 0;
    stringstream numstream(str);
    numstream >> num_songs;
    album.setNumSongs(num_songs);

    while(is >> s)
        album.addSong(s);

    return is;
}
重载>>运算符的时间:

istream &operator>>(istream &is, Time &t)
{
    int duration;
    is >> duration;

    t.setHour((duration / 3600) % 60);
    t.setMinute((duration / 60) % 60);
    t.setSecond(duration % 60);

    return is;
}
openFromFile函数:

void Jukebox::openFromFile()
{
    fstream inFile(INFILE, ios::in);
    Album a;

    if(!albvec.empty())
        albvec.clear();

    while(inFile >> a)
        albvec.push_back(a);

    inFile.close();
}
问题是,我最终在正确的数据成员中找到了第一张专辑的标题和专辑中的第一首歌曲,但所有其余的数据都在songvec中。我有点知道是什么问题,但我很难想出一个方法,当一张新专辑开始的时候,告诉大家。问题应该在相册的>>操作符中的这些行附近

while(is >> s)
    album.addSong(s);

这是大学的“家庭作业”,因此我不允许更改播放列表文件的格式。提前感谢您给我们提供了正确方向的提示和指点。

您应该使用相册中的歌曲数量来阅读它们:

for(unsigned long int i = 0; i < num_songs; ++i)
{
  is >> s;
  album.addSong(s);
}
for(无符号长整数i=0;i>s;
相册。addSong(s);
}

因为在代码中,您将在while循环中读取下一张专辑的名称。

您应该删除
album
numsongs
成员,并改用
songvec.size()
。当你忘记更新一个或另一个成员时,使用一个单独的成员可能会引起问题。是的,我在我的原型代码中使用了这两个。我真的不太清楚为什么我会加上那个numsongs成员。是的,但当然!在我阅读实际歌曲之前,我确实阅读了歌曲的数量。今天脑子不好。对不起,我刚才看到我的代码错了。更正完成。