“文件”对象(即w +,r +)的访问模式之间的区别 [英] Difference between the access modes of the `File` object (ie. w+, r+)

查看:266
本文介绍了“文件”对象(即w +,r +)的访问模式之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中使用文件时, r + w + 模式有什么区别?那么 a + 模式呢? http://www.tutorialspoint.com/ruby/ruby_input_output.htmrel =noreferrer> http://www.tutorialspoint.com/ruby/ruby_input_output.htm



引用:


r
只读模式。文件指针位于文件的开头。这是默认模式。

r +

读写模式。文件指针将在文件的开头。


w

只写模式。如果文件存在,则覆盖该文件。如果文件不存在,则创建一个新的文件进行写入。


w +

读写模式。 如果文件存在,则覆盖现有的文件。如果该文件不存在,则创建一个新的文件进行读写。


a

只写模式。如果文件存在,则文件指针位于文件末尾。也就是说,该文件处于附加模式。如果文件不存在,它将创建一个新的文件进行写入。

a +

读写模式。 如果文件存在,文件指针位于文件的末尾。该文件以附加模式打开。如果文件不存在,它会创建一个新的文件来读写。


(empshasis mine。)



r +,w +和a +都是可读写的。 w +截断文件。 a +追加。如果文件不存在,w +和a +都会创建该文件。)


When using files in Ruby, what is the difference between the r+ and w+ modes? What about the a+ mode?

解决方案

See http://www.tutorialspoint.com/ruby/ruby_input_output.htm

To quote:

r
Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.

r+
Read-write mode. The file pointer will be at the beginning of the file.

w
Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

w+
Read-write mode. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

a
Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+
Read and write mode. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

(empshasis mine.)

r+, w+, and a+ all do read-write. w+ truncates the file. a+ appends. w+ and a+ both create the file if it does not exist.)

这篇关于“文件”对象(即w +,r +)的访问模式之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆