tags:

views:

238

answers:

4

I tried help fopen,

but there is no explanation what t means .

A: 

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

ItzWarty
Not quite - MATLAB treats text mode as "strip newlines from input." See http://www.oit.uci.edu/dcslib/matlab/matlab-v53/help/techdoc/ref/fopen.html
Tim
So it means `text` or `translation`,which one?
It means `text` mode.
Tim
A: 

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.

Tim
Can you elaborate how it "strips newlines from input"?I think it'll cause error by concatenating the tail and head together.
Text mode will cause any instances of `\r\n` on a Windows system to be replaced on read with simply `\n`, and the reverse to happen for a write operation. The docs I provided allude to this; those that @mtrw provided specify this explicitly.
Tim
Does the text mode only work in windows?
It only has an effect in Windows - the `\r` character (a carriage return, or `CR`) generally only appears in "Windows-style" line endings (which consist of `\r\n`, or `CRLF`). Unix line endings contain only a `\n` (line feed, or `LF`). Text mode "works" anywhere - it just doesn't make sense to talk about it in the context of a Unix platform.
Tim
+1  A: 

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.

JS Ng
A: 

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.

Marc