Printing Output to a Text File

is simple. You just have to open() the file, write to it and then close() it. This notebook demonstrates this.

Load Packages


In [1]:
using Dates

include("printmat.jl")   #a function for prettier matrix printing


Out[1]:
printyellow (generic function with 1 method)

open


In [2]:
fh = open("Results/NewTxtFile.txt", "w")        #open the file, "w" for writing

println("NewTxtFile.txt has been created in the subfolder Results. It's still empty.")


NewTxtFile.txt has been created in the subfolder Results. It's still empty.

write


In [3]:
println(fh,"Dogs are ")          #printing to the file, notice the fh   
println(fh,"nicer than cats.")
println(fh,"\n")

x = reshape(1:20,5,4)
printmat(fh,x,width=10,prec=0)             #to pretty print the matrix

close


In [4]:
close(fh)                               #close the file

println("NewTxtFile.txt has been closed. Check it out.")


NewTxtFile.txt has been closed. Check it out.

In [ ]: