Archived

This topic is now archived and is closed to further replies.

psykr

file loading (c++)

Recommended Posts

I''m having a hard time implementing the kind of file management I want in C++. I want my file manager to load small files (say, under 1 MB) straight to memory, so that I can pass a pointer to this raw memory. Is there a "standard" way to do this, aside from reading the file into a buffer, then copying it into memory? Trying to keep my manager OS-independent, what''s an easy way to map a large file to memory? For example, if the file is too large, I don''t want to have to copy 20 MB to another spot in memory, I''d rather load it only when it''s requested. I want the file manager to handle this directly, without the client having to pass information like "request_data( byte 4096, to byte 5000 )". Is there any OS-independent way to implement file mapping?

Share this post


Link to post
Share on other sites
a)

#include <vector>
#include <fstream>
#include <iterator>

std::ifstream( "filename", std::ios::binary );
std::vector<char> data( std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>() );

char* data_ptr = &data[0]; // if you really want a pointer.



b) Write a wrapper around your system''s memory-mapped file API.

And remember that loading a file in a single pass is much faster than doing several disk accesses.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A question. When loading the file as you did above in the constructor of a vector by using streabuf_iterators, is that thought as loading the file in one pass?

Share this post


Link to post
Share on other sites
Yes. Kinda. The program will read as much as can fill the ifstream's read buffer. The number of disk reads is dependent on the relative sizes of the buffer and the file, as well as any OS-side caching.

[edited by - Fruny on January 5, 2004 10:56:13 PM]

Share this post


Link to post
Share on other sites