ifstream::read Question

Discussion in 'C++' started by brad, May 28, 2008.

  1. brad

    brad Guest

    How would I determine the number of bytes that is.read actually read?

    // allocate memory
    char * buffer;

    while (!is.eof())
    {

    buffer = new char [chunk];

    //read data as a block
    is.read(buffer, chunk);

    //write the read data to stdout
    std::cout << buffer;

    delete [] buffer;
    }
     
    brad, May 28, 2008
    #1
    1. Advertising

  2. brad

    Christopher Guest

    On May 28, 9:05 am, brad <> wrote:
    > How would I determine the number of bytes that is.read actually read?
    >
    > // allocate memory
    > char * buffer;
    >
    > while (!is.eof())
    > {
    >
    > buffer = new char [chunk];
    >
    > //read data as a block
    > is.read(buffer, chunk);
    >
    > //write the read data to stdout
    > std::cout << buffer;
    >
    > delete [] buffer;
    > }


    1) Open a web browser and go to www.google.com
    2) Search for C++ ifstream
    3) Click on of the top 4 links
    4) Navigate to member function read
    5) Read the following:

    "The function gcount() is used with input streams, and returns the
    number of characters read by the last input operation."

    Sorry for the sarcasm, I can't help it :)
     
    Christopher, May 28, 2008
    #2
    1. Advertising

  3. brad

    Noah Roberts Guest

    brad wrote:
    > How would I determine the number of bytes that is.read actually read?
    >
    > // allocate memory
    > char * buffer;
    >
    > while (!is.eof())
    > {
    >
    > buffer = new char [chunk];
    >
    > //read data as a block
    > is.read(buffer, chunk);
    >
    > //write the read data to stdout
    > std::cout << buffer;
    >
    > delete [] buffer;
    > }


    http://www.cplusplus.com/reference/iostream/istream/read.html
     
    Noah Roberts, May 28, 2008
    #3
  4. brad

    brad Guest

    > function gcount() is used with input streams, and returns the
    > number of characters read by the last input operation."
    >
    > Sorry for the sarcasm, I can't help it :)


    That's OK... I'm used to it. I overlooked gcount... thanks for taking
    the time to respond.

    Brad
     
    brad, May 28, 2008
    #4
  5. brad

    Jerry Coffin Guest

    In article <g1josh$bkt$>, says...
    > How would I determine the number of bytes that is.read actually read?
    >
    > // allocate memory
    > char * buffer;
    >
    > while (!is.eof())
    > {
    >
    > buffer = new char [chunk];
    >
    > //read data as a block
    > is.read(buffer, chunk);
    >
    > //write the read data to stdout
    > std::cout << buffer;
    >
    > delete [] buffer;
    > }


    While your original question has been answered, I feel obliged to point
    out that the rest of this code falls somewhat short of what you might
    want.

    First of all, almost loop like your:

    while (!is.eof())

    will require extra work to get correct results. The stream's eof() will
    only return true if you'd reached the end of the file before the last
    time you called read.

    Second, you're wasting a lot of time and effort on allocating and
    deleting your buffer. Right now, you're allocating and deleting the
    buffer with each iteration of the loop -- without seeming to accomplish
    anything useful by doing so.

    Finally, read() does NOT append a nul character to the buffer following
    the data it reads, so your use of operator<< (which expects a char* to
    represent a nul-terminated string) can cause a problem as well. Instead
    of using operator<<, you should really use the ostream's write().

    do {
    char buffer[chunk];
    is.read(buffer, chunk);
    std::cout.write(buffer, is.gcount());
    } while (is.gcount() > 0);

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, May 31, 2008
    #5
  6. brad

    sas Guest

    On May 28, 6:08 pm, Christopher <> wrote:
    > On May 28, 9:05 am, brad <> wrote:
    >
    >
    >
    > > How would I determine the number of bytes that is.read actually read?

    >
    > >      // allocate memory
    > >      char * buffer;

    >
    > >      while (!is.eof())
    > >        {

    >
    > >        buffer = new char [chunk];

    >
    > >        //read data as a block
    > >        is.read(buffer, chunk);

    >
    > >        //write the read data to stdout
    > >        std::cout <<  buffer;

    >
    > >        delete [] buffer;
    > >        }

    >
    > 1) Open a web browser and go towww.google.com
    > 2) Search for C++ ifstream
    > 3) Click on of the top 4 links
    > 4) Navigate to member function read
    > 5) Read the following:
    >
    > "The function gcount() is used with input streams, and returns the
    > number of characters read by the last input operation."
    >
    > Sorry for the sarcasm, I can't help it :)


    Stumbled on this post by googling for the problem :)
    Thanks for the helpful post.
     
    sas, Jun 11, 2008
    #6
    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. Gunnar

    ifstream.read manual

    Gunnar, Jul 27, 2003, in forum: C++
    Replies:
    2
    Views:
    8,824
    Jim Fischer
    Jul 28, 2003
  2. Nils Wogatzky

    ifstream - read - problem

    Nils Wogatzky, Sep 8, 2003, in forum: C++
    Replies:
    5
    Views:
    671
    Kevin Goodsell
    Sep 8, 2003
  3. lokb
    Replies:
    5
    Views:
    4,687
    Victor Bazarov
    Jul 8, 2004
  4. Clint Ruen
    Replies:
    4
    Views:
    561
    Richard Herring
    Dec 7, 2004
  5. Replies:
    4
    Views:
    616
Loading...

Share This Page