reading a whole file?

Discussion in 'C++' started by markspace, May 24, 2004.

  1. markspace

    markspace Guest

    Hi all,

    Here's a question I haven't been able to answer on my own. I want to
    read in the entire contents of a file into some structure in memory then
    process it. Like for example read an image into memory, then display it.

    Is there an easy way to read an entire file into memory in C++? I'd
    like to read it into an Array of some type, but anything would do. I'd
    like the result to be contigous in memory (important!) and fairly
    efficient. Strings might work but it's not really a string that I'm
    after, more of an array of byts kind of model.

    Any suggestions?
    markspace, May 24, 2004
    #1
    1. Advertising

  2. markspace wrote:
    > Here's a question I haven't been able to answer on my own. I want to
    > read in the entire contents of a file into some structure in memory then
    > process it. Like for example read an image into memory, then display it.
    >
    > Is there an easy way to read an entire file into memory in C++? I'd
    > like to read it into an Array of some type, but anything would do. I'd
    > like the result to be contigous in memory (important!) and fairly
    > efficient. Strings might work but it's not really a string that I'm
    > after, more of an array of byts kind of model.
    >
    > Any suggestions?


    The member function 'read' of std::ifstream class should probably suffice

    V
    Victor Bazarov, May 24, 2004
    #2
    1. Advertising

  3. markspace

    Phlip Guest

    markspace wrote:

    > Here's a question I haven't been able to answer on my own. I want to
    > read in the entire contents of a file into some structure in memory then
    > process it.


    That structure should be a std::string, without a reason for a more
    difficult structure.

    std::string fileToString(std::string const& name) {
    std::ifstream in(name.c_str());
    return std::string(std::istreambuf_iterator<char>(in),
    std::istreambuf_iterator<char>());
    }

    > Like for example read an image into memory, then display it.
    > Is there an easy way to read an entire file into memory in C++? I'd
    > like to read it into an Array of some type, but anything would do. I'd
    > like the result to be contigous in memory (important!) and fairly
    > efficient. Strings might work but it's not really a string that I'm
    > after, more of an array of byts kind of model.


    Strings are arrays of bytes. Now what system do you envision between your
    file and a display on the screen? If the file is in a graphics format, such
    as PNG, then you'd be much better off researching the easiest possible
    library to re-use. Then it will tell you how it wants to take its input.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, May 24, 2004
    #3
  4. "Phlip" <> wrote in message
    news:p2rsc.20150$...
    > markspace wrote:
    >
    > > Here's a question I haven't been able to answer on my own. I want to
    > > read in the entire contents of a file into some structure in memory then
    > > process it.

    >
    > That structure should be a std::string, without a reason for a more
    > difficult structure.
    >
    > std::string fileToString(std::string const& name) {
    > std::ifstream in(name.c_str());
    > return std::string(std::istreambuf_iterator<char>(in),
    > std::istreambuf_iterator<char>());
    > }


    I think a vector<char> would be better. The OP does say its important that
    the structure be contiguous in memory.

    std::vector<char> fileToVector(std::string const& name) {
    std::ifstream in(name.c_str());
    return std::vector<char>(std::istreambuf_iterator<char>(in),
    std::istreambuf_iterator<char>());
    }

    john
    John Harrison, May 24, 2004
    #4
    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. Replies:
    2
    Views:
    593
    kryptomoon
    Sep 13, 2005
  2. \A_Michigan_User\
    Replies:
    2
    Views:
    887
    \A_Michigan_User\
    Aug 21, 2006
  3. Michael Mair

    Reading whole text files

    Michael Mair, Feb 10, 2005, in forum: C Programming
    Replies:
    50
    Views:
    1,279
    Chris Croughton
    Feb 16, 2005
  4. Roger Pack
    Replies:
    3
    Views:
    95
    Caleb Clausen
    Dec 2, 2009
  5. Replies:
    6
    Views:
    286
    cartercc
    Jun 17, 2008
Loading...

Share This Page