7

I have a library which consumes a FILE * and outputs data to another FILE *.

I want to handle both the input to this library and the output from this library in memory without reading/writing to/from a file on the disk.

We're doing this in iOS - so running the library as a separate app and using stdin/stdout isn't (as far as I know) a viable option.

Steve
  • 31,144
  • 19
  • 99
  • 122
  • there's [fmemopen](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html) and [open_memstream](http://pubs.opengroup.org/onlinepubs/9699919799/functions/open_memstream.html), but I don't know if they are available on iOS – Christoph Apr 20 '12 at 19:48
  • No, they aren't available on iOS or OS X. – Kurt Revis Apr 20 '12 at 20:36

2 Answers2

5

Because ObjC is a superset of C, all you have to do is #import/#include <stdio.h> to gain access to the funopen() which in itself contains the functions readfn, writefn, seekfn, and closefn. And fwopen which has an example showing how to write to two streams at this other SO question.

Mac OSX and iOS don't include fmemopen and open_memstreambecause they are apparently unportable linux functions

As of macos 10.13, ios 11.0, tvos 11.0, and watchos 4.0, fmemopen and open_memstream along with a few other helpful POSIX.1-2008 standard functions are available in stdio.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • @KurtRevis, I have changed my answer accordingly. – CodaFi Apr 20 '12 at 20:44
  • OK then, how exactly do you use fopen() to get a FILE* from a block of memory, not a file? – Kurt Revis Apr 20 '12 at 20:47
  • @KurtRevis fwopen and funopen would work as well for writing, especially to two files as shown [here](http://stackoverflow.com/a/1043871/945847) – CodaFi Apr 20 '12 at 20:49
  • I'm sorry I'm providing such funky answers, I haven't touched IO C functions in a while. – CodaFi Apr 20 '12 at 20:51
  • 2
    *highly unportable* is a bit strong - the functions are available in the GNU, uClibc and Newlib libc implementations and part of POSIX since 2008 – Christoph Apr 20 '12 at 22:02
  • @Christoph to quote the mailing list answerer >Leopard conforms to UNIX03, not POSIX.1-2008. – CodaFi Apr 20 '12 at 22:04
  • 3
    @CodaFi: it did't feel right to me to call something which is available in Linux, Haiku, Cygwin, AmigaOS highly unportable just because it's missing from OSX – Christoph Apr 20 '12 at 22:15
  • The OS X people seem to have a stick up their ass wrt to standards compliance. That they don't conform to POSIX.1 2008 yet doesn't mean that they should not attempt to implement the very simply `fmemopen()` function anyway. – fuz Apr 18 '16 at 12:15
2

Have a look at https://github.com/shyuep/pyhull/tree/master/src/fmemopen, I've tested it myself on a Mac OSX 10.8.2 and it is working ok.

Author states it should also work on iOS.

fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • I've tried compiling the above library on my mac and i get an error `error: no matching function for call to 'funopen'` are there any special flags needed? – pyCthon Nov 10 '13 at 01:26
  • i'm on `Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)` if that helps – pyCthon Nov 10 '13 at 01:28