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?
回答(2 个)
Ben Drebing
2018-1-12
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 个评论
Walter Roberson
2018-1-12
'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.
Walter Roberson
2018-1-12
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.
Ken
2018-1-12
Ken
2018-1-12
Walter Roberson
2018-1-12
No, 'a+' forces all writes to be at the end of the file. You would need 'a' permission for what you are proposing.
Eivind Hennestad
2024-3-1
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);
Eivind Hennestad
2024-3-1
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');
Walter Roberson
2024-3-1
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.
Jan
2018-1-12
0 个投票
Under Windows you can use https://www.mathworks.com/matlabcentral/fileexchange/24671-filetime to set the modification date manually.
类别
在 帮助中心 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!