我正在创建一个应用程序,将某些文件从我们的 ubuntu 服务器复制到外部硬盘(使用 ntfs 格式化)进行备份,其中大多数文件大小都超过 4GB。 问题是有时某些文件丢失(不是所有文件,也不是每种情况)。
这些是我的代码:
char buf[BUFSIZ];
size_t size;
int source = open(c_fileName, O_RDONLY, 0);
int dest = open(c_targetFile, O_WRONLY | O_CREAT /*| O_TRUNC/**/, 0644);
while ((size = read(source, buf, BUFSIZ)) > 0) {
write(dest, buf, size);
*progress+=size;
}
close(source);
close(dest);
在应用程序使用以下代码完成写入后,我尝试检查文件是否存在且大小是否相同:
if (fsource = fopen(c_targetFile, "rb")) {
isSourceFileExists=true;
// check file size
fseek (fsource, 0, SEEK_END); // non-portable
size=ftell(fsource);
fclose(fsource);
}else{
isSourceFileExists=false;
size=0;
}
但问题是有时我无法使用其他计算机找到该文件。
最佳答案
Yesterday, it happen again and i already check using ls and the file is there. But today when I open it at home, I realize it's missing a file out of 24 files.
如果文件首先存在,但在将硬盘插入其他位置(或关闭电源然后再打开)后丢失,则您可能只是没有正确卸载驱动器。
关于c - 外部驱动器上写入的文件丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616468/