我是 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/