如何读取 Crystal 中的文件?
How to read a file in Crystal?
作为 Rubyist 一段时间后,我最近开始使用 Crystal,但我似乎找不到任何关于文件 class 的信息。我想打开并读取一个文件,但出现错误。
file = File.open("ditto.txt")
file = file.read
tequila@tequila-pc:~/code$ crystal fileopen.cr
Error in fileopen.cr:2: wrong number of arguments for 'File#read' (given 0, expected 1)
Overloads are:
- IO::Buffered#read(slice : Bytes)
- IO#read(slice : Bytes)
file = file.read
^~~~
您可能正在寻找 IO#gets_to_end
which reads the entire file as a String
. But you might as well use File.read
file_content = File.read("ditto.txt")
IO#read
是一种更底层的方法,它允许将 IO 的片段读入字节片。
作为 Rubyist 一段时间后,我最近开始使用 Crystal,但我似乎找不到任何关于文件 class 的信息。我想打开并读取一个文件,但出现错误。
file = File.open("ditto.txt")
file = file.read
tequila@tequila-pc:~/code$ crystal fileopen.cr
Error in fileopen.cr:2: wrong number of arguments for 'File#read' (given 0, expected 1)
Overloads are:
- IO::Buffered#read(slice : Bytes)
- IO#read(slice : Bytes)
file = file.read
^~~~
您可能正在寻找 IO#gets_to_end
which reads the entire file as a String
. But you might as well use File.read
file_content = File.read("ditto.txt")
IO#read
是一种更底层的方法,它允许将 IO 的片段读入字节片。