What does fopen('text.txt', 'wt') mean in MATLAB?
I tried 开发者_如何学JAVAhelp fopen
,
but there is no explanation what t
means .
From the documentation (R2009a, Windows):
On UNIX systems, binary and text modes are the same.
On Windows systems, binary and text modes are different. If you are unsure which mode is best for your file, use binary mode. By default,
fopen
opens files for binary read access.In binary mode, read and write operations process all characters in the same manner. In text mode:
Read operations that encounter a carriage return followed by a newline character remove the carriage return from the input.
Write operations insert a carriage return before any newline character in the input.
The UNIX version (R2009b) goes on to add (in doc fopen
):
For better performance, do not use text mode.
It's similar to PHP and other languages in that the t
does stand for "text" mode; however, the meaning is a little different.
In MATLAB, if you open a file in text mode, it strips line endings from input before the lines are processed or manipulated, then readds them for output; binary mode, indicated with a b
, performs no such newline stripping.
See the fopen reference.
from matlab fopen documentation
To open files in text mode, attach the letter 't' to the permission, such as 'rt' or 'wt+'. For better performance, do not use text mode. The following applies on Windows systems, in text mode:
Read operations that encounter a carriage return followed by a newline character ('\r\n') remove the carriage return from the input.
Write operations insert a carriage return before any newline character in the output.
This additional processing is unnecessary for most cases. All MATLAB import functions, and most text editors (including Microsoft Word and WordPad), recognize both '\r\n' and '\n' as newline sequences. However, when you create files for use in Microsoft Notepad, end each line with '\r\n'. For an example, see fprintf.
Stolen from the PHP documentation [yes, this is a different language, but we're talking about the filemode parameter, so it shouldn't be any different]
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
....
Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.
http://php.net/manual/en/function.fopen.php
精彩评论