Creating an in-memory file? (for zlib's gzwrite)
Creating an in-memory file? (for zlib's gzwrite)
- Subject: Creating an in-memory file? (for zlib's gzwrite)
- From: David Thorup <email@hidden>
- Date: Mon, 16 Jun 2003 12:16:54 -0600
I'm trying to create a file, in-memory, so that I can use zlib's
"gzwrite(gzFile file, const voidp buf, unsigned len)" function. I came
across a similar thread in the archives (in-memory files?) where it was
suggested to use shm_open. I tried this out but I can't get it to
work. gzwrite is supposed to return the number of uncompressed bytes
written, if that is not equal to the length of the buffer given then
there's an error. Well, gzwrite is not writing out the entire buffer
and if I try to gzclose the file then that too returns an error. Take
a look at my code below.
If I don't use shm_open, and just write out to a normal file then
everything works, so there must be something wrong with the way I'm
using shm_open. I'm not much of a C coder, so I'm not sure of the best
way to do this. Right now I'm thinking of using pipes (is this better
than shm_open?), but if anyone else has a better suggestion then let me
know.
Thanks!
Here's the code:
Byte* uncompressedBytes;
uLong len, comprLen;
int err;
int memFile, bytesWritten;
gzFile gzmemFile;
// uncompressedBytes and len have been initialized (I've check in the
debugger)
memFile = shm_open("/memfile", O_RDWR | O_CREAT, 0777);
gzmemFile = gzdopen(memFile, "wb");
// if I write to "tmpFile" (instead of an in-memory file) then
everything works
// gzmemFile = gzopen("tmpFile", "wb");
bytesWritten = gzwrite(gzmemFile, uncompressedBytes, len);
if (bytesWritten != len)
NSLog(@"zlib error: bytesWritten != len");
err = gzclose(gzmemFile);
if (err != Z_OK)
NSLog(@"zlib error: %d", err);
// Do some fun stuff
shm_unlink("/memfile");
_____________________________
Dave Thorup
Software Engineer
email@hidden
http://www.kuwan.net
Defaults Manager - The premier editor for Mac OS X's User Defaults /
Preferences database.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.