Skip to content

Instantly share code, notes, and snippets.

@micahpearlman
Created January 18, 2013 03:02
Show Gist options
  • Save micahpearlman/4562041 to your computer and use it in GitHub Desktop.
Save micahpearlman/4562041 to your computer and use it in GitHub Desktop.
Use fmemopen to create a file descriptor (FILE) for data already in memory.
Use fmemopen to create a file descriptor (FILE) for data already in memory.
see: http://www.kernel.org/doc/man-pages/online/pages/man3/fmemopen.3.html
for iOS/OSX version see: https://github.com/jverkoey/fmemopen
@UserXGnu
Copy link

It's important to note that fmemopen(); doesn't provide a file descriptor but rather a file pointer. If fileno() is used in order to retrive the actual file descriptor, it will return -1, since there is no file descriptor at all.

char buffer[128] = { 'a' }
FILE * fp = fmemopen(buffer, sizoef(128), "r+");
int fd = fileno(fp);
printf ("File descriptor: %d\n", fd);

it will print -1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment