Google
 

Monday, January 17, 2011

C++ Primer Answer book for 4rd edition Exercise 8-3

// Exercise 8.3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
ifstream &printOut( ifstream &in);
istream &print( istream &in);
int main(int argc, _TCHAR* argv[])
{
ifstream in("ReadMe.txt");

//ifstream &inf = printOut(in);

//if(inf)
//{
// cout << "Testing the stream input;" << endl;
// string s;
// //Issue here: already clear the stream, but the stream still can't be reused
// while(inf >> s , !inf.eof())
// {
// cout << s << endl;
// }
// inf.close();
//}

ifstream inf1("ReadMe.txt");
print( inf1);


print(cin);
// test the cleared eofbit mark
if(cin.eof())
{
//why it always show it is not cleared here?
cout << "the eof mark is not cleared!" << endl;
}
else
cout << "the eof mark is cleared!" << endl;
int i(0);
cin >> i;
return 0;
}


ifstream &printOut( ifstream &in)
{
string s;
while(in >> s , !in.eof())
{
//print out all to standard output
cout << s << endl;
}
//reset the stream
in.clear( istream::eofbit);
//in.setstate( istream::eofbit);

return in;
}
istream &print( istream &in)
{
string s;
while(in >> s , !in.eof())
{
//print out all to standard output
cout << s << endl;
}
if(in.eof())
{
cout << "The end of file!" << endl;
}
// clear the eofbit mark
in.clear( istream::eofbit);

return in;
}
Angelo Yin

Keep moving forward!

No comments: