CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    16

    Question getline question!

    With the following code, I'm trying to read a .txt file line by line.

    I want to read through the file until the last line.... Is my if statement correct in saying that if getline(is, line) exists? Will it skip every other line? If incorrect, what's a way to do it? Thanks in advance!

    Code:
    
    istream& operator>> (istream &is, const Song &song) // reads in the values of song
    {
    
       if ( getline(is, line) )
      {
          Song *currentSong = new Song(NULL, NULL, NULL);
          getline(inf, currentSong->titleID, "___");
          getline(inf, currentSong->artistID, "___");
          inf.ignore(11, "___");
          getline(inf, currentSong->albumID);
          return is;
      }
    }

  2. #2
    Join Date
    May 2008
    Posts
    96

    Re: getline question!

    Your syntax needs a little help. Also, I think you've mixed some things. To gather information from a line you've read, you'll want to use a stringstream. Finally, you are creating a new Song instead of using the one targeted by the >> operator (which shouldn't be const). Finally, no version of getline() takes a string as its third argument, alas.
    Code:
    istream& operator >> (istream& ins, Song& song)
    {
      string line;
      if (getline(ins, line))
      {
        stringstream ls(line);
        getline( ls, song->titleID, '_' );
        while (ls.peek() == '_') ls.get();  // skip consecutive '_'s
        getline( ls, song->artistID, '_' );
        while (ls.peek() == '_') ls.get();  // skip consecutive '_'s
        getline( ls, song->albumID, '_' );
      }
      return ins;
    }
    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured