JasonBorne JasonBorne - 11 months ago 94
Ruby Question

Error while rewriting from temp file

I'm writing to a file from a temp file, when I try to read the file that has been written from the temp file, it seems to be adding an extra character to the directory called

. (file is passed in through optparse)


require 'tempfile'

PATH = Dir.pwd

def format_file
puts 'Writing to temporary file..'
if File.exists?(OPTIONS[:file])
file = Tempfile.new('file')
IO.read(OPTIONS[:file]).each_line do |s|
File.open(file, 'a+') { |format| format.puts(s) unless s.chomp.empty? }
IO.read(file).each_line do |file|
File.open("#{PATH}/tmp/#sites.txt", 'a+') { |line| line.puts(file) }
puts "File: #{OPTIONS[:file]}, has been formatted and saved as #sites.txt in the tmp directory."
puts <<-_END_
Woah now my friend! I know you're eager to get those vulns;

But file: #{OPTIONS[:file]} doesn't exist or in this directory at least!

What I'm gonna need you to do is go move that file over here.
It's okay, you're forgiven, I'll wait until you return..



ruby whitewidow.rb -f sites.txt

[12:40:43 INFO]Formatting file
[12:40:43 INFO]Writing to temporary file..
[12:40:43 INFO]File: tmp/sites.txt, has been formatted and saved as #sites.txt in the tmp directory.
[12:40:43 INFO]Let's check out this file real quick like..
whitewidow.rb:224:in `read': No such file or directory @ rb_sysopen - C:/Users/Justin/MyScripts/RubySQL/whitewidow/#tmp/#sites.txt (Errno::ENOENT)
#<= Correct path but the '#' in tmp shouldn't be there..

What it does is format the file to remove any empty lines within it (this program doesn't like empty lines) from there it should write to a temp file, rewrite from the temp file back to the original directory (whitewidow/tmp/) and delete the temp file (I know how to do this part).

It seems to me like while rewriting back to the original directory it's adding a
to the directory name (
is actually
) is there a reason that it's adding this?


I fixed it, for some reason the program was adding a # to the path, so I gsubed out the # and it works.