Modifying a memory-mapped data file

I am using memmapfile to managed and update structured data records. After I modify a record in the file, the "modified date" of the file is not updated. I do confirm that the record has been modified. Unfortunately, it seems that the memory-mapped file is not updated by by cloud service, presumably because the file "modified date" is not changed.
Does anyone know how this might be fixed/managed?

Answers (2)

Unfortunately, this is a current limitation of MATLAB. But a quick way to get around this is to open and close it again after you've modify the file so that the modified time stamp gets updated. You can just do something like:
fileID = fopen('MyFile.dat','w');
fclose(fileID);
This will update the date modified without actually changing the file.

8 Comments

'w' Open or create new file for writing. Discard existing contents, if any.
That would definitely change the contents of the file.
You would need to use 'a' or 'a+' or 'A' for this purpose.
As I look further, I see that in POSIX's discussion of fopen(), http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html# that all of the access options involving the 'w' flag require that the file be truncated to zero length and that the modification time be updated. I also see that for 'a' access, the update of the modification time is conditional upon the file not having existed before. Thus as far as POSIX.1 is concerned, if you open an existing file with 'a' or 'a+' then although the file will not get truncated, the modification time will not be updated by the fopen() call itself.
Ben - Thank you for acknowledging the problem and proposing a solution
Walter - THANK YOU for keeping me from wiping-out my dig data file!
Ken
Maybe I should open with 'a+", read the first byte, rewind, and write that value back to the first byte, and close. (?)
No, 'a+' forces all writes to be at the end of the file. You would need 'a' permission for what you are proposing.
I might misunderstand what you are saying here, but "a" opens file for appending data, and "a+" does the same, but also allows reading data? I think what Ken is saying is correct, you need "a+" to also read data in.
This is the code for doing what is proposed:
fileID = fopen(filePath, 'a+');
frewind(fileID); % Go to beginning of file
firstByte = fread(fileID, 1, 'uint8');
frewind(fileID); % Go to beginning of file
fwrite(fileID, firstByte, 'uint8');
fclose(fileID);
Also, for the record and to repeat what is noted in the discussion here, the first solution proposed by Ben is not correct. The following statement will wipe out the contents of the file:
fileID = fopen('MyFile.dat','w');
Ah, if the process is to open a file, read the first byte, rewind, and write back the first byte, then you should open with 'r+' permissions.
Opening with 'a+' would not be desirable because for 'a' and 'a+' there is a hidden fseek() to eof before every write.

Sign in to comment.

Products

Asked:

Ken
on 6 Jan 2018

Commented:

on 1 Mar 2024

Community Treasure Hunt

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

Start Hunting!