file - 如何在 Crystal 中读取文件?
问题描述
在成为 Rubyist 一段时间后,我最近开始接触 Crystal,但我似乎找不到任何关于 File 类的信息。我想打开并读取一个文件,但它给了我一个错误。
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
将整个文件读取为String
. 但你不妨使用File.read
file_content = File.read("ditto.txt")
IO#read
是一种更底层的方法,它允许将 IO 的片段读入字节片。
推荐阅读
- python - 用于具有阈值的多输出图像分类的拆分数据集
- c# - 从一个时区转换到另一个时区时,在本地计算机上找不到时区 id 'Russia TZ 8 Standard Time
- python - 带有初始网络的迁移学习模型的预处理
- html - requests:获取 Google Doc 或 Sheet 的最后修改时间?
- c# - 如何在 RS232 串口和本地笔记本电脑之间获取命令历史记录?
- reactjs - 自定义断点未显示在我的断点选项中
- huggingface-transformers - Transformer-base 或 Transformer-big 有多少个参数和 FLOP?如何计算?
- rdma - RDMA 读写数据放置/可见性语义
- c# - ThirdPersonMovement script Unity
- google-chrome - 如何从开发工具中获取 chrome 深色主题并将其用于 VS 代码?