append new line to textfile

Discussion in 'Ruby' started by Mmcolli00 Mom, Nov 21, 2008.

  1. 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/.
     
    Mmcolli00 Mom, Nov 21, 2008
    #1
    1. Advertising

  2. Mmcolli00 Mom

    Shane Emmons Guest

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

    On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom <>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
     
    Shane Emmons, Nov 21, 2008
    #2
    1. Advertising

  3. Shane Emmons wrote:
    > On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
    > <>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/.
     
    Siep Korteling, Nov 21, 2008
    #3
  4. On Nov 21, 2008, at 4:58 PM, Shane Emmons wrote:
    > On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
    > <>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
     
    Rob Biedenharn, Nov 21, 2008
    #4
  5. Mmcolli00 Mom

    Shane Emmons Guest

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

    On Fri, Nov 21, 2008 at 5:23 PM, Siep Korteling <>wrote:

    > Shane Emmons wrote:
    > > On Fri, Nov 21, 2008 at 4:41 PM, Mmcolli00 Mom
    > > <>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
     
    Shane Emmons, Nov 21, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Veeven
    Replies:
    2
    Views:
    526
    Guest
    Jan 12, 2004
  2. Deivys Ramirez
    Replies:
    3
    Views:
    353
    Asun Friere
    Aug 29, 2007
  3. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  4. micropentium
    Replies:
    8
    Views:
    2,625
    micropentium
    Jan 28, 2010
  5. PerlFAQ Server
    Replies:
    0
    Views:
    390
    PerlFAQ Server
    Feb 24, 2011
Loading...

Share This Page