istream::getline() and getline()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: istream::getline() and getline()

  1. #1
    Join Date
    Oct 2007
    Posts
    35

    istream::getline() and getline()

    Hi.

    Can anybody help explain to me why the first getline() in the code below does not read content from the file through the end of file?

    The second getline() does read the whole file and display it via cout. However, if I omit the line in.clear(), then it displays nothing.

    Why was in.clear() necessary, even when already I close and re open the ifstream?

    Thanks
    Sam

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
      const int SZ = 100; // Buffer size;
      ifstream in("Strfile.cpp"); // Read
      char buf[SZ];
      {
        int i = 1; // Line counter
    
        // A less-convenient approach for line input:
        while(in.get(buf, SZ)) { // Leaves \n in input
          in.get(); // Throw away next character (\n)
          cout << buf << endl; // Must add \n
        }
      } 
    
    
      cout << "\n\n non member get()\n";
      in.close();
      //in.clear(); //??? cant omit this
      in.open("Strfile.cpp");
      string s;
      while(getline(in,s))
      {
    	  cout << s << endl;
      }
    
    } ///:~

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: istream::getline() and getline()

    I'm not quite sure what you're trying to accomplish with the first way of reading the data. You're limiting yourself to 100 characters per line, so assuming you want to append the \n character is not a good method. But anyway, that's not your question. "Doesn't read to the end" isn't quite helpful enough to get an answer. Does it read the whole file minus the last buffer (i.e. less the last line) or something along those lines? Stick to std::getline, you'll be happier.

    The reason you need in.clear() is because it clears all set bit flags that contain state which are still set from the previous file. It's annoying, I know, but if you're re-using your variable to open a new file then just get used to having to clear your flags

    -eli
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    Oct 2007
    Posts
    35

    Re: istream::getline() and getline()

    Thanks Eli, I was assuming that all states get cleared when I closed the stream.

    About my first question, what I was trying to accomplish is to use in.getline() to read the source code and cout them. I forgot to mention that Strfile.cpp was the file that contained the attached source code.

    This is the output from running the program, as you can see the getline() did not read through the end of the source file.


    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;

    non member get()
    Press any key to continue . . .

  4. #4
    Join Date
    Oct 2007
    Posts
    35

    Re: istream::getline() and getline()

    Sorry the correct title of this thread should be istream::get() and getline().

  5. #5
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: istream::getline() and getline()

    You read a blank line... that blank line fills zero characters of the buffer. The get function returns 0 as the # of characters read into the sz buffer. The while loop ends because it thinks it reached EOF.
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  6. #6
    Join Date
    Oct 2007
    Posts
    35

    Re: istream::getline() and getline()

    Thanks much Eli.
    You're right, I better stick with nonmember getline(). : )

    Sam

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center