Ruby中的文件打开模式 [英] File opening mode in Ruby

查看:99
本文介绍了Ruby中的文件打开模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new programmar in Ruby. Can someone take an example about opening file with r+,w+,a+ mode in Ruby? What is difference between them and r,w,a?

请解释,并提供示例.

推荐答案

文件打开模式并不是真正针对ruby的-它们是IEEE Std 1003.1的一部分(

The file open modes are not really specific to ruby - they are part of IEEE Std 1003.1 (Single UNIX Specification). You can read more about it here:

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"不存在,则为纯文本"文件.

Any mode that contains the letter 'b' stands for binary file. If the 'b' is not present is a 'plain text' file.

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

The difference between 'open' and 'open for update' is indicated as:

以更新模式打开文件时(在模式参数中,"+"为第二个或第三个字符)时,可以在关联的流上执行输入和输出.但是,应用程序应确保在没有中间调用fflush()或文件定位函数(fseek(),fsetpos()或rewind())的情况下,输出不直接跟随输入,并且输入不直接跟随除非输入操作遇到文件结尾,否则不会在没有中间调用的情况下调用文件定位功能.

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中的文件打开模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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