From: James Gray Date: 2008-09-24T21:53:07+09:00 Subject: Re: update a field in CSV file using fastercsv On Sep 24, 2008, at 7:16 AM, Pit Capitain wrote: > 2008/9/21 Gregory Brown : >> The reason we need to do things this way is that (at least in Ruby, >> but in most languages I'd imagine), there isn't really a way to >> make a >> change to a file while you're reading it. > > Sorry, Greg, but that's not correct. See the following example. > > Create a file with three lines: > > name = "test.txt" > > File.open(name, "w") do |file| > 3.times do |num| > file.puts "line #{num}" > end > end > > Check the contents of the file: > > puts File.read(name) > > Output: > > line 0 > line 1 > line 2 > > Now read a line, write something, and read a line again: > > File.open(name, "r+") do |file| > puts file.gets > file.puts "new data" > puts file.gets > end This has pretty limited use. It's rare that you can get away with chopping up a file like this, in my opinion. James Edward Gray II