我開發了一個C程序(Linux),該程序創建一個新文件並寫入,之後重啓PC。在C程序中調用的Linux重啓函數導致磁盤上的程序創建文件丟失
重新啓動後,我丟失了由我的程序創建的文件。當我停用重啓功能時,我的程序創建的文件仍然存在。
這種行爲被認爲是對Linux: - OpenWrt的(逆火10.03)VirtualBox上(文件系統EXT2) - 的Linux(Ubuntu的)(文件系統EXT4)
你有這種行爲的解釋,我該如何解決它?
#include <stdio.h>
#include <sys/reboot.h>
int main()
{
FILE *pFile;
char mybuffer[80];
pFile = fopen ("/home/user/Desktop/example.txt","w");
if (pFile == NULL) perror ("Error opening file");
else
{
fputs ("test",pFile);
fclose (pFile);
}
rename("/home/user/Desktop/example.txt","/home/user/Desktop/example123.txt");
reboot(RB_AUTOBOOT);
return 0;
}
自己沖洗(同步)文件:http://linux.die.net/man/2/fsync ... – ChristopheD
我並不反對,但我很驚訝,調用fclose()是不夠的。 – larsks
@larsks查看我的答案。手冊頁明確指出了這一點。雖然它有點奇怪。 – pmr