Archived

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

Viajero

Memory files in C

Recommended Posts

(How) can I make a memory file (FILE *) in C (standard lib)? I need to do both, read from and write to a file. So I guess I could use a temporary FILE *, which is loaded in to memory. Or do I have to open the file with "r+" and mess around with the fseek(), fpos() etc? I'm making a simple dos encrypter/decrypter prog, in case you wondering. [edited by - viajero on January 19, 2003 8:06:21 AM]

Share this post


Link to post
Share on other sites
Erhmm, why don''t you just take your file and use fopen()/fread()/fclose() to load it entirely into memory. And then you have the file contents in memory for you to mess around with until you have to write it out again.

Or have I missed the point of your question?


Jacob Marner, M.Sc.
Console Programmer, Deadline Games

Share this post


Link to post
Share on other sites
Got one more question. If I want to "rewind" the file, is this the correct way to do it?

FILE *f1;
int i;
fpos_t *pos;

f1=fopen("f.txt","r");

fgetpos(f1, pos); //Seems like I''m passing just the value of pos to the function..

while(fgetc(f1)!=EOF) i++;

fsetpos(f1, pos);

Share this post


Link to post
Share on other sites
Yes you can do it that way. But if you want to rewind it all the way to the start the preferred way is to use

fseek(file, 0, SEEK_SET);


Jacob Marner, M.Sc.
Console Programmer, Deadline Games

Share this post


Link to post
Share on other sites