strange ifstream problem

Discussion in 'C++' started by jared.oconnell@gmail.com, Jan 13, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. TB Guest

    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
    TB, Jan 13, 2006
    #2
    1. Advertising

  3. 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
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 13, 2006
    #3
  4. Zara Guest

    On 12 Jan 2006 23:27:09 -0800, 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
    Zara, Jan 13, 2006
    #4
  5. Guest

    ..read did the trick.

    Thankyou and please excuse my ignorance,

    Jared
    , Jan 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    10
    Views:
    10,031
    Buster Copley
    Jul 14, 2003
  2. Nils Wogatzky

    ifstream - read - problem

    Nils Wogatzky, Sep 8, 2003, in forum: C++
    Replies:
    5
    Views:
    649
    Kevin Goodsell
    Sep 8, 2003
  3. Anton Ishmurzin
    Replies:
    7
    Views:
    7,657
    Anton Ishmurzin
    Nov 26, 2003
  4. Alexandros

    Problem with ifstream

    Alexandros, Dec 26, 2003, in forum: C++
    Replies:
    0
    Views:
    321
    Alexandros
    Dec 26, 2003
  5. Joe
    Replies:
    2
    Views:
    728
    Sharad Kala
    Jan 20, 2004
Loading...

Share This Page