By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Alex Vinokur" <al****@big-foot.com> wrote in message news:37*************@individual.net...
[snip]
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[i] << " ";
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 -------
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.