reading a whole file?

M

markspace

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?
 
V

Victor Bazarov

markspace said:
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
 
P

Phlip

markspace said:
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),
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.
 
J

John Harrison

Phlip said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top