C/C++判断文件是否已完全写入

发布于 2024-09-18 19:44:52 字数 1204 浏览 7 评论 0原文

我有一个目录(DIR_A)要从服务器 A 转储到服务器 B,它是 预计需要几周时间。 DIR_A 有普通树 结构,即目录可以包含子文件夹或文件等

目标: 由于 DIR_A 被转储到服务器 B,我将不得不 浏览DIR_A并搜索其中的某些文件(不知道 每个文件的确切名称,因为服务器 A 更改了所有文件的名称 正在发送)。我迫不及待地要处理 DIR_A 中的一些文件。所以,我想 一旦我在服务器 B 上收到这些文件,就开始处理这些文件。

简介: 服务器 A 向服务器 B 发送 DIR_A。预计需要数周时间。 我必须在上传之前开始处理 B 处的文件 完全的。

尝试想法: 我决定编写一个程序来列出 DIR_A 的内容。 我继续查找 DIR_A 的文件夹和子文件夹中是否存在文件。 我想我可能会在 DIR_A 中查找文件的 EOF。如果不存在 那么文件尚未完全上传。我应该等到 EOF 被发现。因此,我继续循环,计算文件的大小并验证 EOF 是否存在。如果是这种情况,那么我开始处理该文件。

为了模拟上述情况,我决定编写并执行一个程序写入 一个文本文件,然后在中间停止它而不等待完成。 我尝试使用下面的程序来确定是否可以找到 EOF。我假设由于我突然结束了写入文本文件的程序,因此 eof 将不存在,因此不应达到输出“EOF FOUND”。我错了,因为这已经达到了。我还尝试了 feof() 和 fseek()。

std::ifstream file(name_of_file.c_str, std::ios::binary);
//go to the end of the file to determine eof
char character;
file.seekg(0, ios::end);
while(!file.eof()){

    file.read(character, sizeof(char));

}
file.close();
std::cout << "EOF FOUND" << std::endl

任何人都可以提供确定文件是否已完全写入的想法吗?

I have a directory (DIR_A) to dump from Server A to Server B which is
expected to take a few weeks. DIR_A has the normal tree
structure i.e. a directory could have subfolders or files, etc

Aim:
As DIR_A is being dumped to server B, I will have to
go through DIR_A and search for certain files within it (do not know the
exact name of each file because server A changes the names of all the files
being sent). I cannot wait for weeks to process some files within DIR_A. So, I want to
start manipulating some of the files once I receive them at server B.

Brief:
Server A sends DIR_A to Server B. Expected to take weeks.
I have to start processing the files at B before the upload is
complete.

Attempt Idea:
I decided to write a program that will list the contents of DIR_A.
I went on finding out whether files exist within folders and subfolders of DIR_A.
I thought that I might look for the EOF of a file within DIR_A. If it is not present
then the file has not yet been completely uploaded. I should wait till the EOF
is found. So, I keep looping, calculating the size of the file and verifying whether EOF is present. If this is the case, then I start processing that file.

To simulate the above, I decided to write and execute a program writing to
a text file and then stopped it in the middle without waiting for completion.
I tried to use the program below to determine whether the EOF could be found. I assumed that since I abrubtly ended the program writing to the text file the eof will not be present and hence the output "EOF FOUND" should not be reached. I am wrong since this was reached. I also tried with feof(), and fseek().

std::ifstream file(name_of_file.c_str, std::ios::binary);
//go to the end of the file to determine eof
char character;
file.seekg(0, ios::end);
while(!file.eof()){

    file.read(character, sizeof(char));

}
file.close();
std::cout << "EOF FOUND" << std::endl

Could anyone provide with an idea of determining whether a file has been completely written or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆蝶 2024-09-25 19:44:52

EOF 只是 C++ 告诉您没有更多数据的方式。没有 EOF“字符”可用于检查文件是否已完全写入。

通常完成此操作的方法是使用一个名称传输文件,即 myfile.txt.transferring,传输完成后,将文件移动到目标主机上(返回到类似 myfile.txt 的文件)。您可以通过使用单独的目录来执行相同的操作。

EOF is simply C++'s way of telling you there is no more data. There's no EOF "character" that you can use to check if the file is completely written.

The way this is typically accomplished is to transfer the file over with one name, i.e. myfile.txt.transferring, and once the transfer is complete, move the file on the target host (back to something like myfile.txt). You could do the same by using separate directories.

许仙没带伞 2024-09-25 19:44:52

C 和 C++ 都没有标准方法来确定文件是否仍然打开以供另一个进程写入。我们也有类似的情况:服务器向我们发送文件,我们必须尽快接收并处理它们。为此,我们使用 Linux 的 inotify< /a> 子系统,配置了 IN_CLOSE_WRITE 事件的监视(文件在打开写入后被关闭),它被包装在 boost::asio::posix::stream_descriptor 中以方便异步性。

根据操作系统的不同,您可能有类似的设施。或者只是像已经建议的那样使用 lsof 。

Neither C nor C++ have a standard way to determine if the file is still open for writing by another process. We have a similar situation: a server that sends us files and we have to pick them up and handle as soon as possible. For that we use Linux's inotify subsystem, with a watch configured for IN_CLOSE_WRITE events (file was closed after having been opened for writing), which is wrapped in boost::asio::posix::stream_descriptor for convenient asynchronicity.

Depending on the OS, you may have a similar facility. Or just lsof as already suggested.

夏至、离别 2024-09-25 19:44:52

所有有限文件都有一个结束。如果一个文件正在由一个进程写入,并且(假设操作系统允许)由另一个进程同时读取(比写入速度更快),则读取进程在读取完所有已写入的字符后将看到 EOF书面。

可能效果更好的是,如果您可以确定一段时间长度,在此期间您可以保证收到大量字节并将它们写入文件(注意操作系统缓冲),那么您可以每次遍历目录一次期间,任何改变其文件大小的文件都可以被认为是未完成。

另一种方法需要操作系统支持:使用 lsof 等工具检查接收进程打开了哪些文件。接收者打开的任何文件都未完成。

All finite files have an end. If a file is being written by one process, and (assuming the OS allows it) simultaneously read (faster than it is being written) by another process,then the reading process will see an EOF when it has read all the characters that have been written.

What would probably work better is, if you can determine a length of time during which you can guarantee that you'll receive a significant number of bytes and write them to a file (beware OS buffering), then you can walk the directory once per period, and any file that has changed its file size can be considered to be unfinished.

Another approach would require OS support: check what files are open by the receiving process, with a tool like lsof. Any file open by the receiver is unfinished.

仅此而已 2024-09-25 19:44:52

在 C 中,我认为在 C++ 中也是一样,EOF 不是一个字符;它是文件所处(或不存在)的条件。就像媒体已删除网络关闭不是一个字符。

In C, and I think it's the same in C++, EOF is not a character; it is a condition a file is (or is not) in. Just like media removed or network down is not a character.

羁〃客ぐ 2024-09-25 19:44:52

这种方法在 Windows10 上适用于我:

std::ifstream loadfile(path_to_file, std::ios::binary);
if (loadfile.seekg(0, loadfile.end).tellg() != -1)  
    std::print("File is ready (copy complete)");
else
    std::print("File not ready (copy still in progress)");

将其放入循环中以所需频率轮询状态,您将看到成功复制或传输后 Windows 复制对话框消失后,控制台输出将从“未就绪”更改到“准备”。

This approach works for me on Windows10:

std::ifstream loadfile(path_to_file, std::ios::binary);
if (loadfile.seekg(0, loadfile.end).tellg() != -1)  
    std::print("File is ready (copy complete)");
else
    std::print("File not ready (copy still in progress)");

Put that in a loop to poll status at your desired frequency and you'll see that as soon as the Windows copy dialog disappears after successful copy or transfer, the console output will change from "not ready" to "ready".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文