Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why `boost::filesystem::exists` return false when a file does exist?

Tags:

c++

boost

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

Here is the issue I have:

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:

Case I:

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.

Case II:

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.

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

like image 822
q0987 Avatar asked Aug 31 '25 16:08

q0987


1 Answers

why boost::filesystem::exists return false when a file does exist?

It won't. The file does not exist.

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.

like image 159
Lightness Races in Orbit Avatar answered Sep 02 '25 06:09

Lightness Races in Orbit