By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

reading a whole file?

P: n/a
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?

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
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/bi...UserInterfaces
Jul 22 '05 #3

P: n/a

"Phlip" <ph*******@yahoo.com> wrote in message
news:P2*******************@newssvr31.news.prodigy. com...
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>(i n),
std::istreambuf_iterator<char>());
}

john
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.