Modifying a memory-mapped data file
Mostrar comentarios más antiguos
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?
Respuestas (2)
Ben Drebing
el 12 de En. de 2018
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 comentarios
Walter Roberson
el 12 de En. de 2018
'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
el 12 de En. de 2018
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
el 12 de En. de 2018
Ken
el 12 de En. de 2018
Walter Roberson
el 12 de En. de 2018
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
el 1 de Mzo. de 2024
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
el 1 de Mzo. de 2024
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
el 1 de Mzo. de 2024
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
el 12 de En. de 2018
0 votos
Under Windows you can use https://www.mathworks.com/matlabcentral/fileexchange/24671-filetime to set the modification date manually.
Categorías
Más información sobre Low-Level File I/O en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!