Swap contents in two files

I have two files, I want to swap the contents of the files in my
program, suggestions?

···

--
Posted via http://www.ruby-forum.com/.

Renaming with a third temp file name is the most efficient if you know
that no file handles are open on these files.

robert

···

2006/12/14, Christopher Latif <christopherl@bredband.net>:

I have two files, I want to swap the contents of the files in my
program, suggestions?

--
Have a look: http://www.flickr.com/photos/fussel-foto/

Christopher Latif wrote:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Why not just rename the files? Or am I missing your meaning?

Tom

Christopher Latif wrote:

I have two files, I want to swap the contents of the files in my
program, suggestions?

I arrived late, here's my untested solution:

sa,sb, nevermind = ARGV

da = File.read(sa)
db = File.read(sb)

File.open(sa,"w") { |f| f.write db }
File.open(sb,"w") { |f| f.write da }

Should work for all but the biggest files.

···

--
Paul Lutus
http://www.arachnoid.com

Robert Klemme wrote:

···

2006/12/14, Christopher Latif <christopherl@bredband.net>:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Renaming with a third temp file name is the most efficient if you know
that no file handles are open on these files.

robert

How the code for that program going to look like?

--
Posted via http://www.ruby-forum.com/\.

Got to see this topic just now. I do agree with Tom's opinion.
Shiwei

Tom Werner wrote:

···

Christopher Latif wrote:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Why not just rename the files? Or am I missing your meaning?

Tom

How abt:
require 'fileutils'

FileUtils.copy_file("a.exe","b.exe")
FileUtils.mv "a.exe.stackdump","a.exe"
FileUtils.mv "b.exe","a.exe.stackdump"

···

On 12/14/06, Christopher Latif <christopherl@bredband.net> wrote:

Robert Klemme wrote:
> 2006/12/14, Christopher Latif <christopherl@bredband.net>:
>> I have two files, I want to swap the contents of the files in my
>> program, suggestions?
>
> Renaming with a third temp file name is the most efficient if you know
> that no file handles are open on these files.
>
> robert

How the code for that program going to look like?

--
gnufied
-----------
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

harp:~ > cat a.rb
   require 'tempfile'
   require 'fileutils'

   def swap a, b
     fu, f = FileUtils, File
     tmpdir = f.dirname b
     t = Tempfile.new f.basename(b), f.dirname(b)
     t.close
     c = t.path
     fu.mv b, c
     fu.mv a, b
     fu.mv c, a
   end

   a, b, ignored = ARGV

   swap a, b

   harp:~ > cat a
   a

   harp:~ > cat b
   b

   harp:~ > ruby a.rb a b

   harp:~ > cat a
   b

   harp:~ > cat b
   a

-a

···

On Thu, 14 Dec 2006, Christopher Latif wrote:

Robert Klemme wrote:

2006/12/14, Christopher Latif <christopherl@bredband.net>:

I have two files, I want to swap the contents of the files in my
program, suggestions?

Renaming with a third temp file name is the most efficient if you know
that no file handles are open on these files.

robert

How the code for that program going to look like?

--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama

Shiwei Zhang wrote:

Got to see this topic just now. I do agree with Tom's opinion.
Shiwei

http://pastie.org/1066992

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Fri, 15 Dec 2006, ara.t.howard@noaa.gov wrote:

a, b, ignored = ARGV

You can also do:

   a, b = *ARGV

(which I know you know but it's just another option for anyone who
doesn't want the extra variable :slight_smile:

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hemant Kumar wrote:

How the code for that program going to look like?

How abt:
require 'fileutils'

FileUtils.copy_file("a.exe","b.exe")
FileUtils.mv "a.exe.stackdump","a.exe"
FileUtils.mv "b.exe","a.exe.stackdump"

I can't get your code to work, got a error message:
/usr/local/lib/ruby/1.8/fileutils.rb:501:in `rename': No such file or
directory - a.stackdump or a (Errno::ENOENT)
        from /usr/local/lib/ruby/1.8/fileutils.rb:501:in `mv'
        from /usr/local/lib/ruby/1.8/fileutils.rb:1379:in
`fu_each_src_dest'
        from /usr/local/lib/ruby/1.8/fileutils.rb:1395:in
`fu_each_src_dest0'
        from /usr/local/lib/ruby/1.8/fileutils.rb:1377:in
`fu_each_src_dest'
        from /usr/local/lib/ruby/1.8/fileutils.rb:490:in `mv'
        from test.rb:4

···

On 12/14/06, Christopher Latif <christopherl@bredband.net> wrote:

--
Posted via http://www.ruby-forum.com/\.

quite right. i've moved to the former because it's self doccumenting : a
crutch for my feeble mind and almost complete lack of doccumentation skills!

:wink:

-a

···

On Fri, 15 Dec 2006 dblack@wobblini.net wrote:

Hi --

On Fri, 15 Dec 2006, ara.t.howard@noaa.gov wrote:

a, b, ignored = ARGV

You can also do:

a, b = *ARGV

(which I know you know but it's just another option for anyone who
doesn't want the extra variable :slight_smile:

--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama

Christopher Latif wrote:

Hemant Kumar wrote:

How the code for that program going to look like?

How abt:
require 'fileutils'

FileUtils.copy_file("a.exe","b.exe")
FileUtils.mv "a.exe.stackdump","a.exe"
FileUtils.mv "b.exe","a.exe.stackdump"

what's stackdump?

···

On 12/14/06, Christopher Latif <christopherl@bredband.net> wrote:

--
Posted via http://www.ruby-forum.com/\.

I usually use

a,b, = *ARGV

Kind of to be sure... :slight_smile:

  robert

···

On 14.12.2006 16:23, ara.t.howard@noaa.gov wrote:

On Fri, 15 Dec 2006 dblack@wobblini.net wrote:

Hi --

On Fri, 15 Dec 2006, ara.t.howard@noaa.gov wrote:

a, b, ignored = ARGV

You can also do:

a, b = *ARGV

(which I know you know but it's just another option for anyone who
doesn't want the extra variable :slight_smile:

quite right. i've moved to the former because it's self doccumenting : a
crutch for my feeble mind and almost complete lack of doccumentation skills!

Hi --

···

On Fri, 15 Dec 2006, ara.t.howard@noaa.gov wrote:

On Fri, 15 Dec 2006 dblack@wobblini.net wrote:

Hi --

On Fri, 15 Dec 2006, ara.t.howard@noaa.gov wrote:

a, b, ignored = ARGV

You can also do:

a, b = *ARGV

(which I know you know but it's just another option for anyone who
doesn't want the extra variable :slight_smile:

quite right. i've moved to the former because it's self doccumenting : a
crutch for my feeble mind and almost complete lack of doccumentation skills!

Come to think of it... you can do:

   a, b = ARGV

I'm not sure why the star presented itself to my brain.

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Heh.. don't worry abt stackdump its the stackdump file of of a.exe,
with whom I am going to swap the content. b.exe was the temporary
file. I would suggest go for Ara's solution, I jumped the gun a tad
too quickly.

but, before running mine script, please put your actual file names...
in stead of dummy one that I have put for you. Obviously, in your
current directory those files are not there and hence the error:

require 'fileutils'

FileUtils.copy_file("src_file","temp_file")
FileUtils.mv "dest_file","src_file"
FileUtils.mv "temp_file","dest_file"

but if you are planning to use, above code, you will have to make sure
that your program doesn't have another temp_file open with same name.

···

On 12/14/06, Christopher Latif <christopherl@bredband.net> wrote:

Christopher Latif wrote:
> Hemant Kumar wrote:
>> On 12/14/06, Christopher Latif <christopherl@bredband.net> wrote:
>>> How the code for that program going to look like?
>>>
>>
>> How abt:
>> require 'fileutils'
>>
>> FileUtils.copy_file("a.exe","b.exe")
>> FileUtils.mv "a.exe.stackdump","a.exe"
>> FileUtils.mv "b.exe","a.exe.stackdump"
>

what's stackdump?

--
gnufied
-----------
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

Yuck. :wink:

James Edward Gray II

···

On Dec 14, 2006, at 9:50 AM, Robert Klemme wrote:

I usually use

a,b, = *ARGV