当文件仍在 OS / 磁盘驱动程序缓存中时是否可以重命名文件?
Is it possible to rename a file when it's still in OS / Disk driver's cache?
例如,我将一个大缓冲区写入一个临时文件,然后立即重命名该文件。会发生什么?
const char* tmp_file = "test.bin.tmp";
const char* real_file = "test.bin";
FILE* fp = fopen(tmp_file, "w+b");
if(fp)
{
fwrite(large_buffer, sizeof(large_buffer), 1, fp);
fclose(fp);
// Here, assume that the data is still in the OS's / Disk driver's cache
rename(tmp_file, real_file);
}
OS会刷新缓存,然后正常执行重命名操作。
例如,我将一个大缓冲区写入一个临时文件,然后立即重命名该文件。会发生什么?
const char* tmp_file = "test.bin.tmp";
const char* real_file = "test.bin";
FILE* fp = fopen(tmp_file, "w+b");
if(fp)
{
fwrite(large_buffer, sizeof(large_buffer), 1, fp);
fclose(fp);
// Here, assume that the data is still in the OS's / Disk driver's cache
rename(tmp_file, real_file);
}
OS会刷新缓存,然后正常执行重命名操作。