Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 无法获取puts以显示read()_Ruby - Fatal编程技术网

Ruby 无法获取puts以显示read()

Ruby 无法获取puts以显示read(),ruby,Ruby,下面是我的代码,它似乎运行良好,并按预期写入文件。但是,我根本无法获取第45行,即put target.read(),以显示target的内容。请帮忙。哦,如果您对我的代码的其他部分有任何评论或建议,我们也将不胜感激 filename = ARGV.first script = $0 puts "We're going to erase #{filename}." puts "If you don't want that, hit CTRL-C." puts "If you do want t

下面是我的代码,它似乎运行良好,并按预期写入文件。但是,我根本无法获取第45行,即put target.read(),以显示target的内容。请帮忙。哦,如果您对我的代码的其他部分有任何评论或建议,我们也将不胜感激

filename = ARGV.first
script = $0

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

print ">>"
STDIN.gets

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

puts "Truncating the file."
target.truncate(target.size)

puts "Now, I'm going to ask for you 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")
puts "Do you want to continue and perform the same"
puts "task with one line of target.write() methods?"
puts "Give me a 'yes' or a 'no'."
print ">>"
answer = STDIN.gets.chomp()
puts "=============================="
if answer == "yes"
target.write("#{line1}\n#{line2}\n#{line3}\n")
end

puts "This is the content of #{filename}:\n"
target = File.open(filename)
# Cannot figure out why the program is not displaying the contents
# of the target file when it's run.
puts target.read()
puts "And finally, we close it."
target.close()

您应该打开
target
进行阅读:

target = File.open(filename, 'r')
根据文档,需要一个整数参数来表示要读取的字节数。与此相反,我建议使用迭代的方法,一行一行地打印。您之前也不必打开该文件,但我想我会以open方式向您显示'r'参数

这就是我要做的:

IO.foreach(filename) do |line|
  puts line
end

此方法在执行块后关闭文件,因此无需显式调用close。在读取之前,请确保在写入文件后将其关闭。

尝试在“w+”中打开文件:

这样,它以读写方式打开文件,将现有文件截断为零长度 或者创建一个新的文件进行读写。因此,不需要在代码后面进行显式截断

完成写入后,文件光标位于文本的末尾。在开始读取之前,应将光标放回第一个字节:

target.rewind

太好了,谢谢。为了更好地理解read()方法,我一定会详细阅读。我来试试你的代码块。再次感谢。@RipRocK如果你有问题,请告诉我。祝你好运,太好了。我将仔细研究你的代码片段,看看能得到什么样的结果。谢谢你。@ RiPrack一旦你得到了一些工作,请考虑发表评论和建议。
target.rewind