3

I am trying to have a FILE* in C that can write to a string buffer in memory rather than a file on disk.

I know that this functionality exists in the fmemopen function, but I am not running my program on Linux so this is not a solution for me. I have seen implementations for Mac as well, but I am programming for a Cortex M4 ARM processor, so I can't use this as well.

Any solutions out there that would potentially work when programming for a Cortex M4?

EDIT: To clarify, I need to end up with a FILE structure because I need to pass a FILE* into a library function I am using (libjpeg's jpeg_stdio_dest() and jpeg_stdio_src() functions) which specifically takes in a FILE* as an argument.

Peter Washington
  • 317
  • 3
  • 10
  • 1
    Just for the record `FILE*` is not a pointer to a file in disk, its a pointer to a `FILE` structure that holds the data required to handle a file descriptor with buffering support. – Havenard Jun 01 '15 at 02:01
  • The fact that you are using an ARM processor isn't quite relevant to this question. More relevant would be the operating system you're targeting, since the operating system provides this kind of functionality, not the processor itself. – Paul92 Jun 01 '15 at 02:02
  • Now back to the question, why exactly you need that if you already have all the data prompty available in memory? – Havenard Jun 01 '15 at 02:02
  • Good point. I edited my question accordingly, Havenard. And Paul92, I am not running any OS on the processor. There is no notion of a filesystem for the program loaded onto the processor (since I am not running an OS on it), which is why I must have the data promptly available in memory. – Peter Washington Jun 01 '15 at 02:13
  • Is this as simple as using `sprintf()` rather than `fprintf()`? – chux - Reinstate Monica Jun 01 '15 at 02:14
  • I need to end up with a 'FILE' structure because I need to pass a `FILE*` into a library function I am using (libjpeg's `jpeg_stdio_dest()` and `jpeg_stdio_src()` functions) which specifically takes in a `FILE*` as an argument. It looks like `sprintf()` only writes to a string, so I don't think I could use this. – Peter Washington Jun 01 '15 at 02:30
  • 2
    Surely libjpeg has functions that work on memory buffers? – nobody Jun 01 '15 at 02:40
  • 1
    Andrew, yes, that turned out to be the solution suggested by one of my friends. Thanks! For anyone who looks at this in the future, libjpeg provides a function called `jpeg_mem_src` that is used for this purpose. It was introduced in libjpeg8 though, so you can't use it with the precompiled libjpeg-turbo binaries. – Peter Washington Jun 01 '15 at 03:05

0 Answers0