Skip to content
Prev 326981 / 398502 Next

writing multiple lines to a file

HI,
May be this helps:
printer1<- file("out1.txt","w")
write(sprintf("This is line %d.\n",1),printer1,append=TRUE) 
write("This is line 2",printer1,append=TRUE)
close(printer1)

#or


?printer1<- file("out1.txt","w")
writeLines("This is line",con=printer1,sep="\n")
writeLines("This is line 2",con=printer1)
?close(printer1)
A.K.


Hello, I am trying to wrote multiple lines to a file, but I only seem to be able to write the last line. 

printer = file("out.txt") 
write(sprintf("This is line %d.\n",1),printer,append=T) 
write("This is line 2.",printer,append=T) 
close(printer) 

How can I fix this? I would like to be able to do this in a for-loop with hundreds of elements.