当文件确实存在时,为什么`boost :: filesystem :: exists`返回false? [英] why `boost::filesystem::exists` return false when a file does exist?

查看:73
本文介绍了当文件确实存在时,为什么`boost :: filesystem :: exists`返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        const boost::filesystem::path fileName( "/tmp/hello.log" );
        if ( boost::filesystem::exists( fileName ) )
        {
            // do sth
        }
        else
        {
            std::cout << "file doesn't exist: " << std::endl;
        }

这是我遇到的问题:

在我可以处理日志文件(即/tmp/hello.log)之前,需要检查该文件是否已完成.如果文件不完整,我将在以后检查文件.

Before I can process the log file(i.e. /tmp/hello.log), I need to check whether or not the file is completed. If the file is not complete, I will check the file later.

要运行仿真,我选择以下方法:

To run simulation, I choose the following methods:

案例1:

第一个日志文件不完整(即末尾没有END)

The log file first is incomplete(i.e. without END as the last line)

$echo "END" >> /tmp/hello.log

我的应用程序按预期运行.换句话说,如果文件不完整,我的应用程序将重试,然后稍后成功处理已完成的日志文件.

My application runs as expected. In other word, my application will try again if the file is incomplete and later successfully process the completed log file.

第二种情况:

第一个日志文件不完整(即,没有END作为最后一行)我使用 vi 手动在末尾插入一行,而应用程序同时保持检查以下几行:

The log file first is incomplete(i.e. without END as the last line) I use vi to manually insert one line in the end while the application at the same time keeps checking the following lines:

        const boost::filesystem::path fileName( "/tmp/hello.log" );
        if ( boost::filesystem::exists( fileName ) )
        {
            // do sth
        }
        else
        {
            std::cout << "file doesn't exist: " << std::endl;
        }

将最后一行附加到文件后,我的应用程序将报告错误并说文件不存在".但是实际上,日志文件在那里.

After I append the last line to the file, my application will report error and say "file doesn't exist". But in fact, the log file is there.

为什么在案例二中,boost函数将返回false,而在案例I中,函数将返回true.

Why in Case II, the boost function will return false while in Case I the function return true.

推荐答案

当文件确实存在时,为什么 boost :: filesystem :: exists 返回false?

不会.该文件不存在.

您说这是可行的,除非您在 vi 中执行编辑.请记住, vi 不是简单的命令行工具,而是功能强大的文本编辑器.可能是使用临时文件(例如,/tmp/hello.log~ )进行修改.在保存更改之前,在/tmp/hello.log 中将找不到这些更改.您应该学习 vi 的文档,以获取有关其工作方式的更多信息.

You say that this works unless you perform the editing in vi. Bear in mind that vi is not a simple command-line tool, but a powerful text editor. It may very well be using a temporary file (say, /tmp/hello.log~) for modifications. Until you save changes, those changes won't be found at /tmp/hello.log. You should study vi's documentation for more information on how it works.

这篇关于当文件确实存在时,为什么`boost :: filesystem :: exists`返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆