2014-02-13 88 views
1

我在桌面上編寫了下面的代碼,它工作正常。我在我的筆記本電腦上下載了它,下載了ruby(v1.9.3),並試圖運行它,但出現以下錯誤。我非常確定這是第一次使用Ruby,但在我第一次運行Ruby時從未在桌面上看到過這個問題。:字符串包含空字節Ruby

C:/Users/Downloads/vscript.rb:18:in 'initialize': string contains null byte (ArgumentError) 
from C:/Users/Downloads/vscript.rb:18:in 'open' 
from C:/Users/Downloads/vscript.rb:18:in 'main' 

線18是File.open行:

File.open("filename", "r") do |f| 

    # Do while there are characters in the text file 
    f.each do |line| 

    # Checks to see if any parts in file match the regex and inform the user 
    if x = line.match(/\d\.\d\.\d{4}\.\d/) 
     puts "#{x} was found in the file." 
    end 
    end 
end 
+1

'vscript.rb'中的編碼是什麼?也許UTF-16? –

+0

它以UTF-8編碼,沒有BOM –

+0

你確定它是UTF-8嗎? UTF-8文本不應包含任何零字節,但UTF-16將包含大量零字節。 –

回答

1

了它。當我最初編寫代碼時,文件名已將文件夾分開。當我在筆記本電腦上下載該文件時,我從使用\的地址欄複製了它的新目錄。改變了這一點,它現在工作正常。