473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading a whole file?

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
3 2935
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
7
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
12
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Ha­rry,St....
2
by: jimmyfishbean | last post by:
Hi, I am using VB6, SAX (implementing IVBSAXContentHandler). I need to extract binary encoded data (images) from large XML files and decode this data and generate the appropriate images onto...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.