From: Tom Reilly Date: 2007-08-27T10:58:23+09:00 Subject: Re: How to append some data at the beginning of a file Here is a small example to write something at the beginning of a file which works fine so long as the length of this item doesn't change. #----------------------------------------------- #timer to track time I work on medicine #version 1.3 8/12/07 # sleep added # total time computed and displayed rw = "r+" $stdout.sync = true begin fo = File.new("time_study.txt",rw) rescue rw = "w+" retry end old_chapter = fo.read(3) if old_chapter == nil fo.write("00\t *** \t \n") p "Start a new file" else puts old_chapter end fo.seek(0,IO::SEEK_SET) time_start = Time.now prev_chapter = fo.read(2) puts "Start at chapter #{prev_chapter}" puts "Enter chapter to start" fo.seek(0,IO::SEEK_SET) chapter = gets.chomp fo.write("#{chapter}") time_end = Time.now fo.seek(0,IO::SEEK_END) fo.write("#{time_start.to_i} #{time_end.to_i} #{(time_end - time_start)}\n") fo.seek(0,IO::SEEK_SET) total_time = 0.0 fo.each do |x| xa = x.split total_time += xa[2].to_f end puts "Total time Hours = #{total_time / 3600.0}" sleep 10 fo.close