# How to create Libc.FILE from a string

**URL:** <https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637>\
**Category:** General Usage\
**Created:** [March 28, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637 "2020-03-28T14:13:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [March 28, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637/1 "2020-03-28T14:13:33Z")

</div>

I’m looking for a solution to the following problem:  
A function in `c` takes a `FILE *rfile` argument. If i want to read file I can do

```julia
open(filename, "r") do file_hdlr
    c_file_hdlr = Libc.FILE(file_hdlr)
    # ccall with c_file_hdlr as Ptr{Cvod}
end

```

however sometimes I have the content of the input file as a `String` and I’d like to avoid writing files e.g. as:

```julia
S = "abc"
open(tempname(), "rw") do file_hdlr
    write(file_hdlr, S)

    c_file_hdlr = Libc.FILE(file_hdlr)
    seek(c_file_hdlr, 0)
    # ccall with c_file_hdlr as Ptr{Cvod}
end

```

Question as in title: how to create `Libc.FILE` from `String` without writing to a temp file?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 28, 2020, 8:02pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637/2 "2020-03-28T20:02:32Z")

</div>

I don’t believe there is a generic way of doing this. If you are on linux then I would look into the C library call `mkfifo` to create a named pipe. Then in Julia (on another thread or maybe in an async task) you open it for writing and write the string. The on your main execution path you open it for reading and pass it to the external library.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 29, 2020, 4:16pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637/3 "2020-03-29T16:16:41Z")

</div>

To elaborate a bit on why this can’t be done, the trouble is that `FILE` is an opaque structure that each libc is free to implement however it wants. Julia’s IO handles, on the other hand, are wrappers around libuv file handles and do not correspond to libc `FILE` handles. They do contain raw `int` file descriptors somewhere, but getting those out in a generic way is not straightforward either. The fifo approach seems like a good one.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [March 29, 2020, 4:34pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637/4 "2020-03-29T16:34:57Z")

</div>

@StefanKarpinski thanks for the answer;  
we will then first try to modify the `c` code (but it’s a mangled spaghetti monster:).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 29, 2020, 7:18pm UTC](https://discourse.julialang.org/t/how-to-create-libc-file-from-a-string/36637/5 "2020-03-29T19:18:43Z")

</div>

Julia IO object, if you includes ones that you can wrap a string in, does not necessarily have a corresponding file descriptor.

Now if you are talking about writing platform specific versions, you can go with `fmemopen` or `open_(w)memstream` to construct a `FILE*` from a string directly.

You also don’t need a named fifo. You can just create an anonymous one (pipe) and you can get the corresponding reader end with `fdopen`.
