473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

strange ifstream problem

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

Jan 13 '06 #1
4 2230
TB
ja************@gmail.com 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.
-----------------------------------------------------------------------
istream_type&
get(char_type* s, streamsize n);

Calls get(s,n,widen("\n")).
-----------------------------------------------------------------------

TB
Jan 13 '06 #2
ja************@gmail.com wrote:
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

Jan 13 '06 #3
On 12 Jan 2006 23:27:09 -0800, ja************@gmail.com wrote:
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
Jan 13 '06 #4
..read did the trick.

Thankyou and please excuse my ignorance,

Jared

Jan 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: SantaClaus | last post by:
Hi all, Im getting an exception in the middle of reading a file and here is my code std::ifstream inf; const int bufSize=1024; char tbuffer; std::string filex = path; // may be C:\test.txt const...
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
0
by: charmeda103 | last post by:
when i run my program it runs with no erorrs but the output screen is giving me strange results here is whats its giving me: CONFERENCE OVERALL RANK TEAM W-L % WINS MARGIN W-L ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.