C++: getline() 忽略前几个字符

发布于 2024-08-24 06:37:15 字数 922 浏览 3 评论 0原文

在我的程序中,fin 是一个ifstream 对象,song 是一个string

当程序运行时,它会打开 music.txt 并从该文件中读取。我尝试使用以下命令读取每一行: getline(fin,song);

我已经尝试了 getline 的所有变体,但它始终忽略每行的前 10 个左右字符在它开始拾取字符之前。例如,如果歌曲名称是“songsongsongsongsongname”,则它可能只会选取“songname”。

有什么想法吗?

这是简化的代码:

 void Playlist::readFile(ifstream &fin, LinkedList<Playlist> &allPlaylists, LinkedList<Songs*> &library) 
{
    string song;
    fin.open("music.txt");  
    if(fin.fail())          
    {
        cout << "Input file failed. No saved library or playlist. Begin new myTunes session." << endl << endl;
    }
    else
    {
        while(!fin.eof() && flag)
        {
                getline(fin, song);     
                cout << song << "YES." << endl;
                }
.....}

In my program, fin is an ifstream object and song is a string.

When the program runs, it opens music.txt and reads from the file. I try to read each line with: getline(fin,song);

I've tried all variations of getline but it keep ignoring the first 10 or so characters of each line before it starts picking up characters. For instance, if the song name is "songsongsongsongsongname," it might only pick up " songname."

Any ideas?

Here's the simplified code:

 void Playlist::readFile(ifstream &fin, LinkedList<Playlist> &allPlaylists, LinkedList<Songs*> &library) 
{
    string song;
    fin.open("music.txt");  
    if(fin.fail())          
    {
        cout << "Input file failed. No saved library or playlist. Begin new myTunes session." << endl << endl;
    }
    else
    {
        while(!fin.eof() && flag)
        {
                getline(fin, song);     
                cout << song << "YES." << endl;
                }
.....}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-08-31 06:37:15

固定版本:

void Playlist::readFile(std::string const& filename, ...) {
    std::ifstream fin(filename.c_str());
    if (!fin) throw std::runtime_error("Unable to open file " + filename);
    for (std::string song; std::getline(fin, song); ) {
        ...
    }
}

最重要的是我删除了 .eof() 的测试。您不能用它来测试您是否可以读取更多内容,也不能用它来测试先前的读取是否成功。验证早期操作是否成功可以通过检查失败标志来完成,或者最常见的是通过测试流本身来完成。

A fixed version:

void Playlist::readFile(std::string const& filename, ...) {
    std::ifstream fin(filename.c_str());
    if (!fin) throw std::runtime_error("Unable to open file " + filename);
    for (std::string song; std::getline(fin, song); ) {
        ...
    }
}

Most importantly I have removed the test of .eof(). You cannot use that for testing if you can read more and you also cannot use it for testing whether the previous read succeeded or not. Verifying that an earlier operation succeeded can be done by checking the fail flag, or most often by testing the stream itself.

薄荷梦 2024-08-31 06:37:15

尝试用这种方法,

...
else
{
    while(fin)
    {
        getline(fin, song);    //read first
        if(!fin.eof() && flag) //detecting eof is meaningful here because
        {                      //eof can be detected only after it has been read
            cout << song << "YES." << endl;
        }
    }
}

Give a try in this way,

...
else
{
    while(fin)
    {
        getline(fin, song);    //read first
        if(!fin.eof() && flag) //detecting eof is meaningful here because
        {                      //eof can be detected only after it has been read
            cout << song << "YES." << endl;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文