Difference between revisions of "Openfile"
From OpenEUO
(Created page with " io.open (filename [, mode]) This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message...") |
|||
Line 15: | Line 15: | ||
This string is exactly what is used in the standard C function fopen. | This string is exactly what is used in the standard C function fopen. | ||
+ | |||
+ | {| class="fopentable" | ||
+ | |- | ||
+ | !colspan=3| mode || description || starts.. | ||
+ | |- | ||
+ | |<tt>r</tt> || <tt>rb</tt> || || open for reading || beginning | ||
+ | |- | ||
+ | |<tt>w</tt> || <tt>wb</tt> || || open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. || beginning | ||
+ | |- | ||
+ | |<tt>a</tt> || <tt>ab</tt> || || open for appending (creates file if it doesn't exist) || end | ||
+ | |- | ||
+ | |<tt>r+</tt> || <tt>rb+</tt> || <tt>r+b</tt> || open for reading and writing || beginning | ||
+ | |- | ||
+ | |<tt>w+</tt> || <tt>wb+</tt> || <tt>w+b</tt> || open for reading and writing. Deletes content and overwrites the file. || beginning | ||
+ | |- | ||
+ | |<tt>a+</tt> || <tt>ab+</tt> || <tt>a+b</tt> || open for reading and writing (append if file exists) || end | ||
+ | |} |
Revision as of 19:23, 7 October 2010
io.open (filename [, mode])
This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message.
The mode string can be any of the following:
- "r": read mode (the default);
- "w": write mode;
- "a": append mode;
- "r+": update mode, all previous data is preserved;
- "w+": update mode, all previous data is erased;
- "a+": append update mode, previous data is preserved, writing is only allowed at the end of file.
- "b": the mode string can also have a 'b' at the end, which is needed to open the file in binary mode.
This string is exactly what is used in the standard C function fopen.
mode | description | starts.. | ||
---|---|---|---|---|
r | rb | open for reading | beginning | |
w | wb | open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. | beginning | |
a | ab | open for appending (creates file if it doesn't exist) | end | |
r+ | rb+ | r+b | open for reading and writing | beginning |
w+ | wb+ | w+b | open for reading and writing. Deletes content and overwrites the file. | beginning |
a+ | ab+ | a+b | open for reading and writing (append if file exists) | end |