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; } } ///:~




Reply With Quote