strange ifstream problem

J

jared.oconnell

Howdy,

I am having a weird problem with ifstream.

ifstream img;
img.open(im_fn.c_str(),ios::in|ios::binary);
char *x = new char[65000];
if(!img) {
cerr << "Error: problem reading img" << endl;
exit(1);
}
img.get(x,65000);
cout << img.tellg() << " " << endl;


The output from this is:

64302


I would have thought I would get 64999. Any further reads cause the
ifstream to fall into fail state (tellg returns -1). The weirdest part
is this occurs only for some (typically large) files and stops at the
different bytes for different files. I have tried both g++ and MS2003
compilers so it is not compiler dependent (as I had initially thought
with a problem this weird).

At the end of my rope. Please help!

regards,

Jared
 
T

TB

(e-mail address removed) sade:
Howdy,

I am having a weird problem with ifstream.

ifstream img;
img.open(im_fn.c_str(),ios::in|ios::binary);
char *x = new char[65000];
if(!img) {
cerr << "Error: problem reading img" << endl;
exit(1);
}
img.get(x,65000);
cout << img.tellg() << " " << endl;


The output from this is:

64302


I would have thought I would get 64999. Any further reads cause the
ifstream to fall into fail state (tellg returns -1). The weirdest part
is this occurs only for some (typically large) files and stops at the
different bytes for different files. I have tried both g++ and MS2003
compilers so it is not compiler dependent (as I had initially thought
with a problem this weird).

-----------------------------------------------------------------------
istream_type&
get(char_type* s, streamsize n, char_type delim);

Extracts characters and stores them into successive locations of an
array whose first element is designated by s. Characters are extracted
and stored until any of the following occurs:

* n-1 characters are stored
* An end-of-file on the input sequence
* The next available input character == delim.
* An Exception

If the function stores no characters, it calls the basic_ios member
function setstate(failbit), which may throw ios_base::failure. In any
case, it stores a null character into the next successive location of
the array.
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Howdy,

I am having a weird problem with ifstream.

ifstream img;
img.open(im_fn.c_str(),ios::in|ios::binary);
char *x = new char[65000];
if(!img) {
cerr << "Error: problem reading img" << endl;
exit(1);
}
img.get(x,65000);

get() stops reading if a newline is encountered, use
read() instead.
cout << img.tellg() << " " << endl;

better use gcount() which provides the number of characters
last read.

[snip]

Regards, Stephan
 
Z

Zara

Howdy,

I am having a weird problem with ifstream.

ifstream img;
img.open(im_fn.c_str(),ios::in|ios::binary);
char *x = new char[65000];
if(!img) {
cerr << "Error: problem reading img" << endl;
exit(1);
}
img.get(x,65000);
cout << img.tellg() << " " << endl;


The output from this is:

64302


I would have thought I would get 64999. Any further reads cause the
ifstream to fall into fail state (tellg returns -1). The weirdest part
is this occurs only for some (typically large) files and stops at the
different bytes for different files. I have tried both g++ and MS2003
compilers so it is not compiler dependent (as I had initially thought
with a problem this weird).

At the end of my rope. Please help!

regards,

Jared

Probably, the size of the fiel you are trying to read is 64302. Have
you checked it?

Regards,
Zara
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top