When using files in Ruby, what is the difference between the r+
and w+
modes? What about the a+
mode?
相关问题
- What is the best way to do a search in a large fil
- Spring Integration - Inbound file endpoint. How to
- How to specify memcache server to Rack::Session::M
- In what practical case bool(std::ifstream) != std:
- Why am I getting a “C compiler cannot create execu
相关文章
- How to replace file-access references for a module
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- What is the correct way to declare and use a FILE
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
See http://www.tutorialspoint.com/ruby/ruby_input_output.htm
To quote:
(empshasis mine.)
r+, w+, and a+ all do read-write. w+ truncates the file. a+ appends. w+ and a+ both create the file if it does not exist.)
Access modes
r+
,w+
anda+
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.Answer: Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of file as well it doesn’t create a new file if such file doesn’t exits while in w+ truncate the content of file as well as create a new file if such file doesn’t exists.
For my own benefit / for reference purposes: