读/写入文件?
“阅读”或“写入”文件是什么意思?我是CS的初学者,并且非常困惑这些术语的含义是什么。 “读”文本文件只是意味着打印它吗?
What does it mean to "read to" or "write to" a file? I am a beginner at CS and am fairly confused what is the meaning of these terms. Does "reading" a text file just mean printing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序通常在RAM领域运行 - 您的所有变量,文字,程序操纵的所有数据都位于此内存中。当您需要使用存储在永久存储(即HDD或SSD)中的文件的内容时,首先需要通过RAM访问它。这样做的最常见方法是将请求发送到HDD或SSD,以返回特定文件的内容,将其存储在RAM中,并具有一个引用此部分内存的变量,以便您的程序可以进一步使用它。这是阅读文件的含义。
我的
project.toml
文件的内容存储在我的SSD中,但是当我调用readlines
时,它会以(通过OS和Filesystem)发送请求(通过OS和Filesystem)此文件要读取为内存。然后,变量pr
是指内存中的这些内容,并允许我们的程序将内容用于我们需要的任何目的。如果我使用
pr = read(“/home/sundar/.julia/emovents/v1.7/project.toml”)
而不是read> read
而不是<代码>读取线),这些内容将仅作为一系列字节返回。但是,由于我打电话给readlines
,因此将内容解释为对UTF-8编码,并将其返回为String
s。读取文件通常也涉及这种解释的选择 - 最终,每个文件都是一系列字节,但是通常更方便地使用包含对这些字节的特定解释的变量。写入文件是相反的过程,即获取一些内存中的数据,并将其发送到您的HDD或SSD(再次通过OS和Filesystem)进行长期存储。
Your program usually operates in the realm of RAM - all your variables, literals, all the data your program manipulates, resides in this memory. When you need to work with the contents of a file that's stored in permanent storage (i.e. HDD or SSD), it first needs to be made accessible via the RAM. The most common way to do this is to send requests to the HDD or SSD to return the contents of a particular file, store those in RAM, and have a variable that refers to this part of memory so that your program can further work with it. This is what is meant by reading a file.
The contents of my
Project.toml
file were stored in my SSD, but when I callreadlines
, that sends a request (via the OS and the filesystem) for the contents of this file to be read into memory. The variablepr
then refers to these contents in the memory, and allows our program to use the contents for whatever purpose we need.If I'd used
pr = read("/home/Sundar/.julia/environments/v1.7/Project.toml")
instead (withread
instead ofreadlines
), these contents would have been returned as just a series of bytes. But since I calledreadlines
, the contents were interpreted as being UTF-8 encoded, and returned asString
s. Reading a file often involves this kind of choice of interpretation too - ultimately, every file is a series of bytes, but often it's more convenient to work with variables that contain a particular interpretation of those bytes.Writing to a file is the opposite process, of taking some data that's in memory, and sending it to your HDD or SSD (again, via the OS and the filesystem) to be stored long term.