473,320 Members | 1,535 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,320 software developers and data experts.

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 7865

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

Similar topics

3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
2
by: nissiml | last post by:
hi, i'm trying to open a asp.net web page that list files from a Windows application like winword and select a file from it . what do i have to do to make it happen, is it simple ? Thanks in...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
1
by: doanwon | last post by:
hello, first post, thanks for reading... Say I create an MFC Dialog program. I then click on an "OnCreateButtons" button to create a long list of MFC buttons/objects, which would then be...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.