Adding to existing .txt file created by writedlm()

question

#1

I’m currently trying to find a solution on how to add to an already existing .txt file; created with writedlm(). I’m passing an array to the file and want to add to the array in the .txt file later on, starting in the next line below the previous array. They both have the same number of columns. I think, I need to do something like:

test=[test4 test2]
6×4 Array{Int64,2}:
1 2 2 2
2 3 3 3
3 4 4 4
4 5 5 5
5 6 6 6
6 7 7 7
writedlm(“mytestfile.txt”, test)

This works so far, I get a readable textfile. Now, to add to it, I think I need to open and then write to it, but I didn’t find the documentation to write() very helpful. I was thinking, maybe there is a possibility to do something like the following (pseudocode!)

test2=[1 2 3 4]
open(“mytestfile.txt”, “a”)
write(test2, add=true)
close(“mytestfile.txt”)
readdlm(mytestfile.txt)
7×4 Array{Int64,2}:
1 2 2 2
2 3 3 3
3 4 4 4
4 5 5 5
5 6 6 6
6 7 7 7
1 2 3 4

Does anybody have an idea, how to do this? Does write() work like this? Thanks for your help!


#2

Yep, you almost got it:

julia> io = open("mytestfile.txt", "a")                                                                                                                                  
IOStream(<file mytestfile.txt>)                                                                                                                                          

julia> writedlm(io, test2)                                                                                                                                               
                                                                                                                                                                         
julia> close(io)                                                                                                                                                         
                                                                                                                                                                         
julia> readdlm("mytestfile.txt")                                                                                                                                         
7×4 Array{Float64,2}:                                                                                                                                                    
  4.85572e18  -4.16586e18  7.04904e18   3.09679e18                                                                                                                       
 -5.46165e18   9.9803e17   1.23416e18   4.61344e18                                                                                                                       
  3.57079e18   9.61015e17  8.53084e18   1.67331e18                                                                                                                       
 -1.96476e17   2.5882e18   4.04351e18  -1.62127e18                                                                                                                       
  1.62979e18   8.28968e18  2.49656e18  -2.50475e18                                                                                                                       
  2.66834e18   1.65467e18  2.62867e18  -7.93416e18                                                                                                                       
  1.0          2.0         3.0          4.0                                                                                                                              

quite nice its to use the do-syntax:

julia> open("mytestfile.txt", "a") do io
       writedlm(io, test2)
       end                                                                                                                                                               

#3

Thank you so much! Good to know I wasn’t far of… I didn’t know I could just use writedlm() again, and the bit with the IOStream :wink: Thanks!