# Adding to existing .txt file created by writedlm()

**URL:** <https://discourse.julialang.org/t/adding-to-existing-txt-file-created-by-writedlm/6907>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 6, 2017, 1:03pm UTC](https://discourse.julialang.org/t/adding-to-existing-txt-file-created-by-writedlm/6907 "2017-11-06T13:03:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 6, 2017, 1:03pm UTC](https://discourse.julialang.org/t/adding-to-existing-txt-file-created-by-writedlm/6907/1 "2017-11-06T13:03:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 6, 2017, 2:03pm UTC](https://discourse.julialang.org/t/adding-to-existing-txt-file-created-by-writedlm/6907/2 "2017-11-06T14:03:45Z")

</div>

Yep, you almost got it:

```julia
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
julia> open("mytestfile.txt", "a") do io
       writedlm(io, test2)
       end                                                                                                                                                               

```

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [November 6, 2017, 4:07pm UTC](https://discourse.julialang.org/t/adding-to-existing-txt-file-created-by-writedlm/6907/3 "2017-11-06T16:07:29Z")

</div>

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 😉 Thanks!
