470,815 Members | 1,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Open a file from memory?

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;
}

--
----------------------------
Kresimir Spes
Cateia Games
http://www.cateia.com
Nov 14 '05 #1
6 7576

"DigitalDragon" <kr***@cateia.com> wrote
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?
Nov 14 '05 #2
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?

--
----------------------------
Kresimir Spes
Cateia Games
http://www.cateia.com


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?

Nov 14 '05 #3
DigitalDragon wrote:
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.
Nov 14 '05 #4
"DigitalDragon" <kr***@cateia.com> wrote in message
news:c7**********@ls219.htnet.hr...
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());

Nov 14 '05 #5
"DigitalDragon" <kr***@cateia.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #6
-wombat- <sc****@cs.ucla.edu> writes:
DigitalDragon wrote:
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.


I think he has a (non-standard) library routine (which he isn't able
to modify) that takes a FILE* argument and expects to read data from
an external file.

The ideal solution would be to have a version of the library routine
that reads from an array rather than from an external file, but that
may not be an option.

fmemopen() would probably an good solution if it were standard, but it
isn't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Murasama | last post: by
6 posts views Thread by Charles Morrall | last post: by
2 posts views Thread by nissiml | last post: by
4 posts views Thread by DyslexicAnaboko | last post: by
1 post views Thread by doanwon | last post: by
16 posts views Thread by graham.keellings | last post: by
18 posts views Thread by Coffee Pot | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.