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

42 views (last 30 days)
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 Comment
Philip Borghesani
Philip Borghesani on 1 Jul 2014
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.

Sign in to comment.

Accepted Answer

dpb
dpb on 30 Jun 2014
Edited: dpb on 1 Jul 2014
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.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!