Advertisement

Memory files in C

Started by January 19, 2003 06:49 AM
4 comments, last by Viajero 21 years, 10 months ago
(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]
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
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Advertisement
Ahh, fread(), thank you .. Also I could just make an integer table and load the file to memory manually using fgetc. Doh..
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);
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
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Cheers

This topic is closed to new replies.

Advertisement