# How to share file with C?

**URL:** https://discourse.julialang.org/t/how-to-share-file-with-c/128325
**Category:** General Usage
**Created:** [April 23, 2025, 10:13am UTC](https://discourse.julialang.org/t/how-to-share-file-with-c/128325 "2025-04-23T10:13:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)
#### Post date: [April 23, 2025, 10:13am UTC](https://discourse.julialang.org/t/how-to-share-file-with-c/128325/1 "2025-04-23T10:13:39Z")

</div>

My program contains some libraries written in C, and both the Julia part and the C part will write to the same log file. How can I share the file between Julia and C?  
I didn’t find a way to use `FILE*` in Julia. The file descriptor can be used by `open(fd::OS_HANDLE) -> IO`, but the [document](https://docs.julialang.org/en/v1/base/io-network/) claims that `Do not call this on a handle that's already owned by some other part of the system`, so I suspect it cannot be used either?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 23, 2025, 10:49am UTC](https://discourse.julialang.org/t/how-to-share-file-with-c/128325/2 "2025-04-23T10:49:31Z")

</div>

The easiest solution is not to share a file handle. Either write a function to write to log in C and call that function from Julia, or do it the other way round.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [April 23, 2025, 11:11am UTC](https://discourse.julialang.org/t/how-to-share-file-with-c/128325/3 "2025-04-23T11:11:13Z")

</div>

The docstring also says “Call `open(Libc.dup(fd))` to avoid the ownership capture of the original handle” so try that?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 23, 2025, 12:52pm UTC](https://discourse.julialang.org/t/how-to-share-file-with-c/128325/4 "2025-04-23T12:52:19Z")

</div>

> [@jsjie](#):
>
> I didn’t find a way to use `FILE*` in Julia.

Did you see [`Libc.FILE`](https://docs.julialang.org/en/v1/base/libc/#Base.Libc.FILE)?

That should do exactly what you want. Open the file `io = open(...)` on the Julia side, and pass `Libc.File(io)` as a `FILE*` to the C side so that C can use it for writing. Close it on the Julia side when you are done.
