Open a file from memory?

D

DigitalDragon

hi,

i would like to use stream I/O functions to open a file from data that i
have already stored in memory.

eg.

char some_text[]="Test";

int main()
{
FILE* stream=fopen(some_text,"rb");
return 0;
}
 
K

Kip Neuhart

DigitalDragon said:
i would like to use stream I/O functions to open a file from data that i
have already stored in memory.
Okay.

eg.
<snip code>

As long as the string resolves to a valid file, there's not a problem. Be
sure to check the return value of fopen. Were you having any problems in
particular with what you're trying to do?
 
D

DigitalDragon

er, i used a bad example. the point i'm trying to make is if i have already
loaded a file into memory, and now i want to pass the FILE pointer to
another library to read that file, but i dont want to reread if from disk,
but rather from memory. is that even posibile?
 
W

-wombat-

DigitalDragon said:
er, i used a bad example. the point i'm trying to make is if i have
already loaded a file into memory, and now i want to pass the FILE pointer
to another library to read that file, but i dont want to reread if from
disk, but rather from memory. is that even posibile?

Either (a) you've already read the file and unserialized the data, which now
is stored in data structures, or (b) you read the file's contents into
memory and now you want to unserialize using the file's contents in memory.

If you've already done (a), you don't have a problem. If (b) is your
problem, then the answer is no, stdio doesn't do that.
 
S

Siemel Naran

i would like to use stream I/O functions to open a file from data that i
have already stored in memory.

eg.

char some_text[]="Test";

int main()
{
FILE* stream=fopen(some_text,"rb");
return 0;
}

This post is cross-posted to C and C++.

For C and C++, read the entire file into an array buffer. You can usually
determine the length of the file by calling fseek with SEEK_END to go to the
end of the file, ftell to get the character position (though this is not
required to return the end of file character position, but seems to under
Windows at least). Construct an array of this size, then read the entire
file into this array with fread.

In C++ you can use iostreams to the the same, but use istream::read instead
of fread, etc.

Or use std::istringstream or even std::istrstream.

std::ifstream infile("file.txt");
std::string string;
string.reserve(...);
std::copy(istream_iterator<char>(infile.rdbuf()), istream_iterator<char>(),
std::back_inserter(string));
std::istringstream instring(string);

Only thing it seems a tad inefficient as you have 2 copies of string. Of
course use create a std::auto_ptr<string> and release it afterwards, but
it's clumsy. I wish you could do

std::istringstream instring(string, std::istringstream::swapstring());
 
K

Keith Thompson

DigitalDragon said:
er, i used a bad example. the point i'm trying to make is if i have already
loaded a file into memory, and now i want to pass the FILE pointer to
another library to read that file, but i dont want to reread if from disk,
but rather from memory. is that even posibile?

So the data you have in memory is the content of the file, not its name.

No, there's no way to do that in standard C (other than by writing the
data to an external file and reading it back in). Some
implementations may provide this as an extension (<OT>fmemopen</OT>),
but of course any code that uses such an extension is non-portable.

A slightly more portable approach might be to copy the data to an
external file on a filesystem that's implemented in memory (if your
system provides such a thing). It's "slightly more portable" in the
sense that, if the system doesn't provide an in-memory filesystem, you
can change the file name and accept the poorer performance.

I see you cross-posted this to comp.lang.c and comp.lang.c++. I'm
posting from comp.lang.c. It's possible that C++ provides a standard
way to do what you want. If anyone wants to discuss it, please post
only to comp.lang.c++; here in comp.lang.c we're a bit sensitive about
topicality.
 

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
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top