Reading a binary file.

Discussion in 'C++' started by Jason Heyes, Oct 25, 2005.

  1. Jason Heyes

    Jason Heyes Guest

    I want to read the binary contents of a file whose size is over 1 megabyte.
    I tried to use this function.

    bool read_file(const char *fname, std::vector<char> &data)
    {
    std::ifstream in(fname);
    if (!in.is_open())
    return false;

    char block[4096];
    while (in.read(block, 4096))
    data.insert(data.end(), block, block + 4096);
    data.insert(data.end(), block, block + in.gcount());
    return true;
    }

    It stops after reading 4198 bytes. Can someone explain this?
    Jason Heyes, Oct 25, 2005
    #1
    1. Advertising

  2. Jason Heyes wrote:
    >
    > I want to read the binary contents of a file whose size is over 1 megabyte.
    > I tried to use this function.
    >
    > bool read_file(const char *fname, std::vector<char> &data)
    > {
    > std::ifstream in(fname);
    > if (!in.is_open())
    > return false;
    >
    > char block[4096];
    > while (in.read(block, 4096))
    > data.insert(data.end(), block, block + 4096);
    > data.insert(data.end(), block, block + in.gcount());
    > return true;
    > }
    >
    > It stops after reading 4198 bytes. Can someone explain this?


    First of all, you should open the file in binary mode.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 25, 2005
    #2
    1. Advertising

  3. Jason Heyes

    mlimber Guest

    Jason Heyes wrote:
    > I want to read the binary contents of a file whose size is over 1 megabyte.
    > I tried to use this function.
    >
    > bool read_file(const char *fname, std::vector<char> &data)
    > {
    > std::ifstream in(fname);
    > if (!in.is_open())
    > return false;
    >
    > char block[4096];
    > while (in.read(block, 4096))
    > data.insert(data.end(), block, block + 4096);
    > data.insert(data.end(), block, block + in.gcount());
    > return true;
    > }
    >
    > It stops after reading 4198 bytes. Can someone explain this?


    Try this:

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <exception>
    using namespace std;

    bool read_file( const char* fname, vector<char>& data )
    {
    // Open in binary mode and at the end
    ifstream input( fname, ios::binary | ios::ate );

    // Enable exceptions on failure
    input.exceptions( ios::badbit | ios::failbit );

    try
    {
    // Get the current position in the file
    const ifstream::pos_type size = input.tellg();

    // Return to the beginning
    input.seekg( 0, ios::beg );

    // Prepare the buffer
    data.resize( vector<char>::size_type(size) );

    // Finally, read the data
    input.read( &data[0], streamsize(size) );
    }
    catch( exception& e )
    {
    cerr << "Read failed: " << e.what() << endl;
    return false;
    }
    return true;
    }

    You probably also will want to validate the conversions between
    pos_type, streamsize, and size_type.

    Cheers! --M
    mlimber, Oct 25, 2005
    #3
    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. Neo
    Replies:
    0
    Views:
    466
  2. El Durango

    reading binary file.

    El Durango, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    7,545
    Gordon Beaton
    Dec 8, 2004
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    912
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,640
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    723
Loading...

Share This Page