c - 外部驱动器上写入的文件丢失

标签 c linux file copy

我正在创建一个应用程序,将某些文件从我们的 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/

相关文章:

linux - 需要与linux命令对应的Windows CMD命令

c# - 将文件复制到不同的目的地每个线程是否线程安全

c - C99 中的哪项规定禁止通过 typedef 定义函数?

c++ - 从 dll 中获取 DLL 的名称

linux - TCP:已连接的套接字已被内核重置?

linux - FTDI FT232R强制冲洗FIFO

在同一变量中强制转换 void 指针会导致编译错误

c - 使用变量按名称搜索枚举?

C++ 通过参数写入文件或控制台

c++ - 如何在 C++ 中正确读取和写入二进制文件中的对象