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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With