-
October 9th, 2007, 02:02 PM
#1
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;
}
} ///:~
-
October 9th, 2007, 02:14 PM
#2
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
-
October 9th, 2007, 02:27 PM
#3
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 . . .
-
October 9th, 2007, 02:35 PM
#4
Re: istream::getline() and getline()
Sorry the correct title of this thread should be istream::get() and getline().
-
October 9th, 2007, 02:43 PM
#5
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.
-
October 9th, 2007, 02:55 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|