473,462 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7879

"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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.