Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > append new line to textfile

Reply
Thread Tools

append new line to textfile

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      11-21-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Shane Emmons
Guest
Posts: n/a
 
      11-21-2008
[Note: parts of this message were removed to make it a legal post.]

On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom <(E-Mail Removed)>wrote:

> 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
> --
> Posted via http://www.ruby-forum.com/.
>
>

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

--
Shane Emmons

 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      11-21-2008
Shane Emmons wrote:
> On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
> <(E-Mail Removed)>wrote:
>
>> 'completed' starttime 05:00 endtime 23:00
>>

> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      11-21-2008
On Nov 21, 2008, at 4:58 PM, Shane Emmons wrote:
> On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
> <(E-Mail Removed)>wrote:
>> 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
> --
> Shane Emmons



Eek! No, that's for a read/write file. You want a mode of 'a' for
append. All write operations will first reposition to the end-of-file.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Shane Emmons
Guest
Posts: n/a
 
      11-21-2008
[Note: parts of this message were removed to make it a legal post.]

On Fri, Nov 21, 2008 at 5:23 PM, Siep Korteling <(E-Mail Removed)>wrote:

> Shane Emmons wrote:
> > On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
> > <(E-Mail Removed)>wrote:
> >
> >> 'completed' starttime 05:00 endtime 23:00
> >>

> > 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
> --
> Posted via http://www.ruby-forum.com/.
>
>

whoops, sorry about that.

--
Shane Emmons

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.2 How do I change, delete, or insert a line in a file, or append to the beginning of a file? PerlFAQ Server Perl Misc 0 02-24-2011 11:00 PM
Append value on the command line to a predefined macro in the makefile micropentium C Programming 8 01-28-2010 06:48 PM
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM
Pythonic way of reading a textfile line by line without throwing anexception Deivys Ramirez Python 3 08-29-2007 02:22 AM
How do I append and modify an XML file on-line? Veeven XML 2 01-12-2004 09:37 PM



Advertisments