Prev: What is the share mode fopen() uses?
Next: VS: operator <<(...const std::string&) does not call ->rdbuf()
From: Ray on 14 Nov 2009 13:56 Hello, The intent of the following code is to read the first 2 characters from each line until EOF is reached. It does this correctly on the first line but stores an empty string for all subsequent lines. Of course I an accomplish this task in other ways but I was wondering why this approach didn't work. Thanks char buf[512]; while (!cin.getline(buf, 3).eof()) { cout << buf << '\n'; cin.ignore(100, '\n'); }
From: Scot Brennecke on 14 Nov 2009 16:11 Ray wrote: > Hello, > > The intent of the following code is to read the first 2 characters from each > line until EOF is reached. It does this correctly on the first line but > stores an empty string for all subsequent lines. Of course I an accomplish > this task in other ways but I was wondering why this approach didn't work. > > Thanks > > > char buf[512]; > > while (!cin.getline(buf, 3).eof()) > { > cout<< buf<< '\n'; > cin.ignore(100, '\n'); > } > This is why I don't like the iostream classes. After the initial getline, the state has been set with failbit. It must be reset or something. I hate these classes and never use them. :)
From: David Wilkinson on 14 Nov 2009 16:21 Ray wrote: > Hello, > > The intent of the following code is to read the first 2 characters from each > line until EOF is reached. It does this correctly on the first line but > stores an empty string for all subsequent lines. Of course I an accomplish > this task in other ways but I was wondering why this approach didn't work. Try like this #include <iostream> #include <string> using namespace std; int main() { string s; while (getline(cin, s) && !s.empty()) { cout.write(s.c_str(), 2) << "\n"; } return 0; } -- David Wilkinson Visual C++ MVP
From: Ray on 14 Nov 2009 17:54 "David Wilkinson" wrote: > Ray wrote: > > Hello, > > > > The intent of the following code is to read the first 2 characters from each > > line until EOF is reached. It does this correctly on the first line but > > stores an empty string for all subsequent lines. Of course I an accomplish > > this task in other ways but I was wondering why this approach didn't work. > > Try like this > > #include <iostream> > #include <string> > using namespace std; > > int main() > { > string s; > while (getline(cin, s) && !s.empty()) > { > cout.write(s.c_str(), 2) << "\n"; > } > return 0; > } > > -- > David Wilkinson > Visual C++ MVP > . > David, Thanks for your suggestion. However, my issue here is not how to accomplish the task, which I can do, but rather, what precisely is wrong with the approach I took in the code sample I provided. Ray
From: Mateusz Loskot Mateusz on 15 Nov 2009 14:38 "Scot Brennecke" wrote: > Ray wrote: > > Hello, > > > > The intent of the following code is to read the first 2 characters from each > > line until EOF is reached. It does this correctly on the first line but > > stores an empty string for all subsequent lines. Of course I an accomplish > > this task in other ways but I was wondering why this approach didn't work. > > > > Thanks > > > > > > char buf[512]; > > > > while (!cin.getline(buf, 3).eof()) > > { > > cout<< buf<< '\n'; > > cin.ignore(100, '\n'); > > } > > > > This is why I don't like the iostream classes. After the initial > getline, the state has been set with failbit. It must be reset or > something. I hate these classes and never use them. :) This is a well-known and standard behaviour and Stephan T. Lavavej explains it extensively here: ID:351636 "STL: ifstream::getline() doesn't set eof flag" https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=351636 Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org
|
Next
|
Last
Pages: 1 2 Prev: What is the share mode fopen() uses? Next: VS: operator <<(...const std::string&) does not call ->rdbuf() |