C++阅读播放列表没有专辑的特定分隔符

标签 c++ c++11

我正在创建一个 C++ 程序来模拟自动点唱机,到目前为止一切正常。但是,我应该从文件中读取专辑,但鉴于播放列表文件必须使用的格式,我无法想出一种在从文件中读取专辑时分隔专辑的方法。

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

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;
        ...
};

还有一个名为 Album 的类,相关部分如下:

class Album
{
    private:
        std::string album_title;
        int numsongs;
        std::vector<Song> songvec;
        ...
};

为歌曲重载 >> 运算符:

istream &operator>>(istream &is, Song &song)
{
    Time t;
    string str;

    getline(is, str, DELIM);
    song.setTitle(str);

    getline(is, str, DELIM);
    song.setArtist(str);

    is >> t;
    song.setDuration(t);

    return is;
}

相册重载 >> 运算符:

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 中的所有其余数据。我有点知道出了什么问题,但我很难想出一种方法来告诉内播新专辑何时开始。问题应该出在 Album 的 >> 运算符中的这些行附近。

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

这是大学的“家庭作业”,因此我不能更改播放列表文件的格式。在此先感谢您提供正确方向的任何提示和指示。

最佳答案

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

for(unsigned long int i = 0; i < num_songs; ++i)
{
  is >> s;
  album.addSong(s);
}

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

关于C++阅读播放列表没有专辑的特定分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657465/

相关文章:

c++ - 为什么 fstream.read 和 fstream.write 使用 char 而不是 unsigned char?

c++ - 当正则表达式匹配重叠时会发生什么?

c++ - 非类型参数取决于它后面的参数

C++继承 - 将 parent 更改为祖 parent

字符串中的 C++ 元音,禁止比较

c++ - std::pair: 过于严格的构造函数?

c++ - 如何将未知数量的元素输出到标准容器

c++ - 返回指向类成员函数的指针

c++ - extern "C"仅在函数声明中需要吗?

c++ - 向现有 XML 文档添加节点