append new line to textfile

M

Mmcolli00 Mom

I want to create a text file and append a new line to the text file
everytime a new logline is completed, my program iterates and gives a
new log every few hours.

this is an example of my logline:
logline: 'completed' starttime 04:00 endtime 23:00

my goal is to have a text file populated with the following
'completed' starttime 04:00 endtime 22:00
'completed' starttime 06:00 endtime 01:00
'completed' starttime 05:00 endtime 23:00
'completed' starttime 09:00 endtime 23:00

so far this is my method...yes, I am a beginner. Thanks in advance. MC

File.open('logfile.txt')do |f1|
f1.logline
end
 
S

Shane Emmons

[Note: parts of this message were removed to make it a legal post.]

I want to create a text file and append a new line to the text file
everytime a new logline is completed, my program iterates and gives a
new log every few hours.

this is an example of my logline:
logline: 'completed' starttime 04:00 endtime 23:00

my goal is to have a text file populated with the following
'completed' starttime 04:00 endtime 22:00
'completed' starttime 06:00 endtime 01:00
'completed' starttime 05:00 endtime 23:00
'completed' starttime 09:00 endtime 23:00

so far this is my method...yes, I am a beginner. Thanks in advance. MC

File.open('logfile.txt')do |f1|
f1.logline
end
File.open('logfile.txt', 'w+') do |f1| f1.write(logline)
end
 
S

Siep Korteling

Shane said:
File.open('logfile.txt', 'w+') do |f1| f1.write(logline)
end
This will destroy the content of logfile.txt. For appending to a file
instead of overwriting it, use "a"

3.times do |n|
logline = "logline #{n}"
File.open('D:/temp/logfile1.txt', 'w+') do |f1|
f1.puts(logline)
end
File.open('D:/temp/logfile2.txt', 'a') do |f1|
f1.puts(logline)
end
end

hth,

Siep
 
S

Shane Emmons

[Note: parts of this message were removed to make it a legal post.]

This will destroy the content of logfile.txt. For appending to a file
instead of overwriting it, use "a"

3.times do |n|
logline = "logline #{n}"
File.open('D:/temp/logfile1.txt', 'w+') do |f1|
f1.puts(logline)
end
File.open('D:/temp/logfile2.txt', 'a') do |f1|
f1.puts(logline)
end
end

hth,

Siep
whoops, sorry about that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top