I assume that `Libc.FILE(foo)` creates a new file descriptor that is independent of `file`, and which needs to be closed as well.
-erik On Wed, Mar 9, 2016 at 3:07 PM, Samuel Massinon <[email protected]> wrote: > Tried looking for a solution but couldn't find anything. If I'm doing > anything wrong please let me know. :) > > If I run the following, > foo = open("foo" ,"w") > ccall(:fprintf, Int, (Ptr{Void}, Cstring), Libc.FILE(foo).ptr, "hello, > world\n") > flush(foo) > close(foo) > foo will exists as a file in my current directory, but will not contain > "hello, world\n", only when I exit julia will it write to foo. > > When I add > ccall(:fflush, Int, (Ptr{Void},), C_NULL) > at the end, then foo will contain "hello, world\n". > > Here's the example output. > samuelmassinon ~$ cat foo > cat: foo: No such file or directory > samuelmassinon ~$ julia > _ > _ _ _(_)_ | A fresh approach to technical computing > (_) | (_) (_) | Documentation: http://docs.julialang.org > _ _ _| |_ __ _ | Type "?help" for help. > | | | | | | |/ _` | | > | | |_| | | | (_| | | Version 0.5.0-dev+3075 (2016-03-09 17:54 UTC) > _/ |\__'_|_|_|\__'_| | Commit 597dc7b (0 days old master) > |__/ | x86_64-apple-darwin15.2.0 > > julia> foo = open("foo" ,"w") > IOStream(<file foo>) > > julia> ccall(:fprintf, Int, (Ptr{Void}, Cstring), Libc.FILE(foo).ptr, > "hello, world\n") > 13 > > julia> flush(foo) > IOStream(<file foo>) > > julia> close(foo) > > shell> cat foo > > julia> exit() > samuelmassinon ~$ cat foo > hello, world > > > I don't think I should have to use fflush to write to a file from c. Is > there a better way of doing this that isn't hacky? > > Thanks! -- Erik Schnetter <[email protected]> http://www.perimeterinstitute.ca/personal/eschnetter/
