A
Alex Vinokur
I have several questins concerning a program below.
------ foo.cpp : BEGIN ------
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[64];
ifstream infile ("foo");
assert (infile);
assert (infile.is_open());
system ("uname -smo");
system ("od -c foo");
cout << endl;
cout << "BEFORE: in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
infile.get (buf, sizeof(buf));
cout << endl;
cout << "FIRST : in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
cout << "eof = " << infile.eof() << ", fail = " << infile.fail() << endl;
cout << "<" << buf << ">" << endl;
infile.get (buf, sizeof(buf));
cout << endl;
cout << "SECOND: in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
cout << "eof = " << infile.eof() << ", fail = " << infile.fail() << endl;
cout << "<" << buf << ">" << endl;
return 0;
}
------ foo.cpp : END --------
------ Run : BEGIN ------
CYGWIN_NT-5.0 i686 Cygwin
0000000 A B \r \n C D E \r \n 1 2 3 4 \r \n
0000017
BEFORE: in_avail = 15, gcount = 0
FIRST : in_avail = 10, gcount = 2
eof = 0, fail = 0
<AB>
SECOND: in_avail = 10, gcount = 0
eof = 0, fail = 1
<>
------ Run : END --------
Questions.
1. Why in FIRST: (in_avail + gcount) != 15 ? (in_avail == 10, gcount == 2)
2. Why did the program fail in SECOND?
3. How to read a file using ifstream::get(char*buffer, streamsize num)?
------ foo.cpp : BEGIN ------
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buf[64];
ifstream infile ("foo");
assert (infile);
assert (infile.is_open());
system ("uname -smo");
system ("od -c foo");
cout << endl;
cout << "BEFORE: in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
infile.get (buf, sizeof(buf));
cout << endl;
cout << "FIRST : in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
cout << "eof = " << infile.eof() << ", fail = " << infile.fail() << endl;
cout << "<" << buf << ">" << endl;
infile.get (buf, sizeof(buf));
cout << endl;
cout << "SECOND: in_avail = " << infile.rdbuf()->in_avail() << ", gcount = " << infile.gcount() << endl;
cout << "eof = " << infile.eof() << ", fail = " << infile.fail() << endl;
cout << "<" << buf << ">" << endl;
return 0;
}
------ foo.cpp : END --------
------ Run : BEGIN ------
CYGWIN_NT-5.0 i686 Cygwin
0000000 A B \r \n C D E \r \n 1 2 3 4 \r \n
0000017
BEFORE: in_avail = 15, gcount = 0
FIRST : in_avail = 10, gcount = 2
eof = 0, fail = 0
<AB>
SECOND: in_avail = 10, gcount = 0
eof = 0, fail = 1
<>
------ Run : END --------
Questions.
1. Why in FIRST: (in_avail + gcount) != 15 ? (in_avail == 10, gcount == 2)
2. Why did the program fail in SECOND?
3. How to read a file using ifstream::get(char*buffer, streamsize num)?