cfile-pointer

Using a C string like a FILE*


I have a C function that reads a stream of characters from a FILE*.

How might I create a FILE* from a string in this situation?

Edit:

I think my original post may have been misleading. I want to create a FILE* from a literal string value, so that the resulting FILE* would behave as though there really was a file somewhere that contains the string without actually creating a file.

The following is what I would like to do:

void parse(FILE* f, Element* result);

int main(int argc, char** argv){
    FILE* f = mysteryFunc("hello world!");
    Element result;
    parse(f,&result);
}

Solution

  • Standard C provides no such facility, but POSIX defines the fmemopen() function that does exactly what you want.