Behavior of ifstream::get(char*buffer, streamsize num)

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)?
 
A

Alex Vinokur

Alex Vinokur said:
3. How to read a file using ifstream::get(char*buffer, streamsize num)?
[snip]



------ foo.cpp : BEGIN -----
#include <cassert>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv)
{

cout << "YOUR COMMAND LINE: ";
for (int i = 0; i < argc; i++) cout << argv << " ";
cout << endl;
cout << endl;

if (argc < 2)
{
cerr << "USAGE: " << argv[0] << " <file-name>" << endl;
return 1;
}
assert (argc > 1);

char buf[4];

const string filename (argv[1]);

ifstream infile (filename.c_str());
assert (infile);
assert (infile.is_open());

system ("uname -smo");
system (string("od -c " + filename).c_str());

cout << endl;

ostringstream oss;
const int newline_int_symbol (int ('\n'));
while (infile.get (buf, sizeof(buf)))
{
oss << buf;
if (infile.peek() == newline_int_symbol)
{
oss << char(infile.get());
}
}
cout << "<" << oss.str() << ">" << endl;

cout << endl;
cout << "File size = " << oss.str().size() << endl;

infile.close();
assert (!infile.is_open());

return 0;
}

------ foo.cpp : END -------


------ Run Logs : BEGIN -----

// GNU gcc/gpp 3.4.1 (DJGPP)

// -----
// Run-1
// -----
YOUR COMMAND LINE: a.exe foo.t1

CYGWIN_NT-5.0 i686 Cygwin
0000000 a b c d e f \r \n g h \r \n i k l \r
0000020 \n
0000021

<abcdef
gh
ikl


// -----
// Run-2
// -----
File size = 14
YOUR COMMAND LINE: a.exe foo.t2

CYGWIN_NT-5.0 i686 Cygwin
0000000 x y z u v t \r \n w q \r \n r s p
0000017

<xyzuvt
wq
rsp>

File size = 13



// -----
// Run-3
// -----
YOUR COMMAND LINE: a.exe foo.b1

CYGWIN_NT-5.0 i686 Cygwin
0000000 A B C D E F \n J H \n I K L \n
0000016

<ABCDEF
JH
IKL
File size = 14


// -----
// Run-4
// -----
YOUR COMMAND LINE: a.exe foo.b2

CYGWIN_NT-5.0 i686 Cygwin
0000000 X Y Z U V T \n W Q \n R S P
0000015

<XYZUVT
WQ
RSP>

File size = 13

------ Run Logs : END -------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top