What is the difference between fopen( 'r'),fopen('r','b') ?

31 次查看(过去 30 天)
When i try to open a file in different way, i get different ans. what it represent? As per my understanding from documents, 'b' represent machine format. b represets bigendian format. By default it will take little indian format. so second one takes little endian format. When i go through about big and little indian, it is memory representation format. what is the difference between the values in 1 and 2.
1. fid = fopen('train-images-idx3-ubyte', 'r', 'b')
header = fread(fid, 1, 'int32')
header =
2051
2. fid = fopen('train-images-idx3-ubyte', 'r')
header = fread(fid, 1, 'int32')
header =
50855936
  1 个评论
Philip Borghesani
Just be careful 'rb' in MATLAB is not the same as 'rb' in C. In C on Windows b means binary mode which is the default in MATLAB, to get text mode fopen (the default in C on Windows) use rt in MATLAB.
C has no equivalent to the machine format options in MATLAB.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2014-6-30
编辑:dpb 2014-7-1
Storage order in memory of which byte in memory is the most/least significant. See the following link for details...
ADDENDUM
That, of course, is for the syntax of your case 1) which, I now note is NOT the same as that of the subject line of the question;, ie,
fid=fopen(filename,'r','b');
is nothing at all like
fid=fopen(filename,'rb');
--the two 'b' 's are entirely different animals owing to position. In the former, it's the optional third argument in the syntax
[FID, MESSAGE] = fopen(FILENAME,PERMISSION,MACHINEFORMAT);
so it's the MACHINEFORMAT parameter, in the latter it's the second (also optional) argument as in
[FID, MESSAGE] = fopen(FILENAME,PERMISSION)
so it's part of the PERMISSIONs string in which case it is, as Philip notes, indicating a stream ('binary') file format as opposed to ASCII ('text') as given by the 't' substring.
Again, see
doc fopen
and read all the supplementary description for the various parameters for the details. The short story is that the 't' indicates text mode that forces the cr/lf pair for \n on Windows that is superfluous in Unix-like OS platforms.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by