# Appending new formatted lines in a file using writedlm()?

**URL:** <https://discourse.julialang.org/t/appending-new-formatted-lines-in-a-file-using-writedlm/121915>\
**Category:** General Usage\
**Created:** [October 29, 2024, 2:03pm UTC](https://discourse.julialang.org/t/appending-new-formatted-lines-in-a-file-using-writedlm/121915 "2024-10-29T14:03:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ValeFed](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valefed/32/213003_2.png) [@ValeFed](https://discourse.julialang.org/u/ValeFed)\
**Post date:** [October 29, 2024, 2:03pm UTC](https://discourse.julialang.org/t/appending-new-formatted-lines-in-a-file-using-writedlm/121915/1 "2024-10-29T14:03:29Z")

</div>

Hi there. My ingredients are some coordinates and a function N that returns a 4-element Vector. I need to create a txt file that has as colums x,y, N[1], N[2], N[3] and N[4].  
Two other important info are that N is created inside a loop on the x,y couples because there should be a very large number of x,y couples, and that I need this file to be nicely formatted because it needs to be read by an external software.

This is a semplified version of the code I use:

```julia
using DelimitedFiles
using Printf

include(shapef.jl)
npoin=10
x=rand(10)
y=rand(10)

shapeFile="shapef.txt"
fmt(a,b,c,d,e,f)=@sprintf("%3.1f %3.1f %5.2f %5.2f %5.2f %5.2f",a,b,c,d,e,f)

file=open("file.txt","a")
for ipoin=1:npoin
    N=shapef(x[ipoin],y[ipoin])
    println(file,fmt(x[ipoin],y[ipoin],N[1],N[2],N[3],N[4]))
end

close(file)

```

Where shapef.jl contains the following function:

```julia
function shapef(s,t)
    N=zeros(nnode) 
    N[1]=0.25*(1+s)*(1+t)
    N[2]=0.25*(1-s)*(1+t)
    N[3]=0.25*(1-s)*(1-t)
    N[4]=0.25*(1+s)*(1-t)
    return N
end

```

This thing works more or less, but I would prefer to have a better formatting, so I would like to do the same thing with writedlm, do you know if it would be possible, and eventually how?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [October 30, 2024, 5:55pm UTC](https://discourse.julialang.org/t/appending-new-formatted-lines-in-a-file-using-writedlm/121915/2 "2024-10-30T17:55:59Z")

</div>

Hi, and welcome to the Julia community!

> [@ValeFed](#):
>
> I would prefer to have a better formatting, so I would like to do the same thing with writedlm

How exactly would you like to improve the formatting?

You could use

```julia
open("file.txt", "a") do file
    writedlm(file, begin
        N = shapef(x[ipoin], y[ipoin])
        (x[ipoin], y[ipoin], N[1], N[2], N[3], N[4])
    end for ipoin = 1:npoin)
end

```

but this does not round the floats. You can still do this manually, of course, e.g. via

```julia
open("file.txt", "a") do file
    writedlm(file, begin
        N = shapef(x[ipoin], y[ipoin])
        (@sprintf("%3.1f", x[ipoin]), 
         @sprintf("%3.1f", y[ipoin]), 
         @sprintf("%5.2f", N[1]), 
         @sprintf("%5.2f", N[2]),
         @sprintf("%5.2f", N[3]), 
         @sprintf("%5.2f", N[4]))
    end for ipoin = 1:npoin)
end

```

(or using Format.jl), but this is already very similar to the `println` solution.
