2015-04-12 40 views
1

這裏是我的代碼:讀取文件(紅寶石堅硬方式EX16)

filename = ARGV.first 

puts "We're gong to erase #{filename}" 
puts "If you don't want that, hit CTRL-C (^C)." 
puts "If you do want that, hit RETURN." 

$stdin.gets 

puts "Opening the file..." 
target = open(filename, 'w') 

puts "Truncating the file. Goodbye!" 
target.truncate(0) 

puts "Now I'm going to ask you for three lines." 

print "line 1: " 
line1 = $stdin.gets.chomp 
print "line 2: " 
line2 = $stdin.gets.chomp 
print "line 3: " 
line3 = $stdin.gets.chomp 

puts "I'm going to write these to the file." 

target.write(line1) 
target.write("\n") 
target.write(line2) 
target.write("\n") 
target.write(line3) 
target.write("\n") 


print target.read 
puts "And finally, we close it." 


target.close 

我試圖把它寫,然後閱讀。如果我在腳本的底部再次執行target.close,然後再執行target = open(filename),它將起作用。有另一種方法嗎?

我看到this關於python的文章解釋你需要在寫入文件後關閉文件。這同樣適用於Ruby嗎?我需要使用同花順嗎?

另外我應該閱讀和關閉後使用括號?這個例子沒有。

+0

這不是一個語言問題,更是一個文件系統/操作系統問題。你在做什麼操作系統? Windows具有強制鎖定,這意味着如果您打開了文件,則在關閉文件之前無法再打開它。 Unix(包括Mac)將允許您隨意地在文件上塗寫文件。 – Schwern

+0

這個「Ruby hard way」代碼看起來不像Ruby,它看起來像是從Python逐行直譯的代碼。 – steenslag

+0

「Learn Ruby The Hard Way」似乎是[「Learn Python The Hard Way」](http://learnpythonthehardway.org/book/ex16.html)的直接翻譯。 @steenslag是對的,這不是很好的Ruby。我會避免它。我建議使用Learn Python The Hard Way來學習編程技術(文件操作技術是普遍的),並使用其他方法學習Ruby,如[原始Pickaxe](http://ruby-doc.com/docs/ProgrammingRuby/) 。我不*推薦[更新的Ruby編程Ruby 2.0](http://www.amazon.com/gp/review/RBK4K4T9YDA9L/ref=cm_cr_pr_rvw_ttl?ie=UTF8&ASIN=1937785491)。 – Schwern

回答

1

有兩種方法可以解決這個問題。您可以像打開文件一樣打開文件進行寫入,寫入文件,關閉文件並重新打開文件進行閱讀。這可以。關閉文件會將其刷新到磁盤,然後重新打開它將使您回到文件的開頭。

或者,您可以打開一個文件,用於讀取和寫入,並在文件內手動移動,如編輯器中的光標。執行此操作的選項在IO.new中定義。

您的代碼的問題是這樣的。

target.write("\n") 

print target.read 

在這一點上,你一直在寫這個文件。 target文件指針指向文件的末尾,就像編輯器中的光標。當你target.read它將讀取文件的結尾,所以你什麼都沒有。您必須先回到rewind的文件開頭。

target.write("\n") 
target.rewind 
print target.read 

您還必須打開文件進行閱讀和寫作。 w+可以做到這一點,併爲你截斷文件。

puts "Opening the file..." 
target = File.open(filename, 'w+') 

這是一種先進的技術,最經常有用當你想在整個讀寫過程,以確保沒有其他人可以在文件上工作,而你要挺住文件的鎖。通常你在閱讀和寫作時都會這樣做。例如,如果你在一個你想讀取的文件中有一個計數器,然後增加,並確保沒有人可以寫入。

def read_and_update_counter 
    value = 0 

    # Open for reading and writing, create the file if it doesn't exist 
    File.open("counter", File::RDWR|File::CREAT, 0644) {|f| 
    # Get an exclusive lock to prevent anyone else from using the 
    # file while we're updating it (as long as they also try to flock) 
    f.flock(File::LOCK_EX) 

    # read the value 
    value = f.read.to_i 

    # go back to the beginning of the file 
    f.rewind 

    # Increment and write the new value 
    f.write("#{value + 1}\n") 

    # Flush the changes to the file out of the in-memory IO cache 
    # and to disk. 
    f.flush 

    # Get rid of any other garbage that might be at the end of the file 
    f.truncate(f.pos) 
    } 
    # File.open automatically closes the file for us  

    return value 
end 

3.times { puts read_and_update_counter } 
+1

OP沒有編寫代碼,它是一個教程(並不是一個很好的IMHO)。 – steenslag

+0

你喜歡的任何教程? – user4584963

+0

@ user4584963 [原創編程Ruby](http://ruby-doc.com/docs/ProgrammingRuby/)。避免編程Ruby 2.0,完全不同的書。 – Schwern