Suppose I have the following code:
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,1,'short',10,'ieee-le')
Then this would open an earlier specified file, skip the first 10 bytes and write 1 into the following two.
But what happens to the first 10 bytes, assuming the opened file did not exist before? If I were to access one, what would I end up getting and why?
In these kind of question, the easiest way to find out is usually to try it out yourself if it's not too complex. Now you indicated you are using Linux, may be you can repeat the test on your platform and see if the results match.
For Windows platform, the skip
value:
00
(probably what the OS assign as new value for a file)Examples:
This code:
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,1,'short',10,'ieee-le')
fclose(fid)
Produces the following file (seen in a hex editor):
And if you have more than one value to write:
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)
You still get 10x 00
values before each short
value you actively write:
This was the case for a newly created file. Let's see what happend to an existing file:
%% Let's create a file full of `FF`
FFarray = uint8(ones(36,1)*255) ;
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,FFarray,'uint8')
fclose(fid)
which gives:
Now by using the same code as before (with the permission
set to w
):
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)
Yes, we still get the same thing. Now this is consistent with the MATLAB documention for the permission you indicated:
w
=> Open or create new file for writing. Discard existing contents, if any.
If you simply change that permission to r+
(Open file for reading and writing.):
fid = fopen(my_filename,'r+','ieee-le','ISO-8859-1');
fwrite(fid,[1 2 3],'short',10,'ieee-le')
fclose(fid)
You only overwrite the non-skipped values:
From the POSIX documentation:
The
fseek()
function shall allow the file-position indicator to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.
Thus, assuming MATLAB's fwrite
uses fseek
to skip byes (which is highly likely), then the skipped bytes past the end of the file will be filled with zeros on any POSIX architecture (Linux, MacOS). This is not necessarily the case for Windows, which is not POSIX.
A quick test on MacOS confirms this behavior:
fn = 'test.bin';
fid = fopen(fn,'wb');
fwrite(fid,1,'uchar',10);
fclose(fid);
fid = fopen(fn,'r');
fread(fid,Inf,'uchar')
fclose(fid);
output:
ans =
0
0
0
0
0
0
0
0
0
0
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With