You are correct, julia> open("foo" ,"w") do file libcf = Libc.FILE(file) ccall(:fprintf, Int, (Ptr{Void}, Cstring), libcf.ptr, "hello, world\n") display(readstring("foo")) close(libcf) display(readstring("foo")) end "" "hello, world\n"
julia> On Wednesday, March 9, 2016 at 5:43:06 PM UTC-6, Erik Schnetter wrote: > > 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 > <samuel....@gmail.com <javascript:>> 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 <schn...@gmail.com <javascript:>> > http://www.perimeterinstitute.ca/personal/eschnetter/ >