Difference between the access modes of the `File` object (ie. w+, r+)

前端 未结 4 1052
清酒与你
清酒与你 2020-12-02 09:47

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

4条回答
  •  既然无缘
    2020-12-02 10:26

    Access modes r+, w+ and a+ opens the file in read and write mode, but with the following difference:

    r+ starts at beginning of file, but will not create a new file if it doesn't exists.

    w+ truncates existing file to zero length if the file exists, otherwise creates a new file.

    a+ starts at end of file if file exists, otherwise creates a new file.

提交回复
热议问题