Swap contents in two files

C

Christopher Latif

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

Robert Klemme

2006/12/14 said:
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
 
C

Christopher Latif

Robert said:
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?
 
H

hemant

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"
 
A

ara.t.howard

How the code for that program going to look like?

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
 
D

dblack

Hi --

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 :)


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)
 
A

ara.t.howard

Hi --



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 :)

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!

;-)

-a
 
R

Robert Klemme

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!

I usually use

a,b, = *ARGV

Kind of to be sure... :)

robert
 
D

dblack

Hi --

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)
 
T

Tom Werner

Christopher said:
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
 
C

Christopher Latif

Hemant said:
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
 
H

hemant

what's stackdump?

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top