Ruby中的文件打开模式

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

请解释一下,并举例说明。

文件打开模式并不是特定于ruby的 – 它们是IEEE Std 1003.1( 单一UNIX规范 )的一部分。 你可以在这里读更多关于它的内容:

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”不存在则是“纯文本”文件。

“打开”和“打开更新”之间的区别表示为:

当以更新模式打开文件(模式参数中的第二个或第三个字符为“+”)时,可以在关联的stream上执行input和输出。 但是,应用程序应确保输出不会直接跟随input,而不需要插入fflush()或文件定位函数(fseek(),fsetpos()或rewind())的中间调用,input不会紧跟在后面除非input操作遇到文件结束,否则无需对文件定位function进行干预调用。