ruby - Ruby 中的文件打开模式

标签 ruby file posix

我是 Ruby 的新程序员。有人可以举例说明在 Ruby 中使用 r+,w+,a+ 模式打开文件吗?它们和r,w,a有什么区别?

请解释,并举例说明。

最佳答案

文件打开模式并不是真正特定于 ruby​​ - 它们是 IEEE Std 1003.1 ( Single UNIX Specification ) 的一部分。您可以在这里阅读更多相关信息:

http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html

r or rb
    Open file for reading.

w or wb
    Truncate to zero length or create file for writing.

a or ab
    Append; open or create file for writing at end-of-file.

r+ or rb+ or r+b
    Open file for update (reading and writing).

w+ or wb+ or w+b
    Truncate to zero length or create file for update.

a+ or ab+ or a+b
    Append; open or create file for update, writing at end-of-file.

任何包含字母'b'的模式都代表二进制文件。如果“b”不存在,则为“纯文本”文件。

'open' 和 'open for update' 的区别表示为:

When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, the application shall ensure that output is not directly followed by input without an intervening call to fflush() or to a file positioning function ( fseek(), fsetpos(), or rewind()), and input is not directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

关于ruby - Ruby 中的文件打开模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7085595/

相关文章:

ruby-on-rails - Rails 导出 csv,包括连接表属性

python - 如何更新Python文件中的特定行?

c - 如何在 POSIX-C 中复制 'which' 命令的行为?

windows - 为什么这个批处理文件会产生额外的、意外的、不需要的字符?

c - 共享内存中的 POSIX 未命名信号量未响应发布或等待

sockets - FIN_WAIT2 状态是否曾经由于关闭连接发起者而出现?

ruby-on-rails - Ruby On Rails是否已为企业准备好了?

python - Ruby 中 subprocess.Popen() 的等价物?

ruby - 在 ruby​​ 中创建带括号的符号

c - 程序不会停止读取文件