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

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:

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:

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?

Hi, and welcome to the Julia community!

How exactly would you like to improve the formatting?

You could use

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

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.