FileSystem - Test if file exists without opening the file?
Hello, I can see from a FileSystem example how to check for the existence of a directory. What I would like to be able to do is to check for the existence of a file but I couldn't figure out how to do it using exists(). The way that I currently check for a file's existence is by opening the file. I wonder if boost::filesystem can test for a file's existence more efficiently than having to open it like I do in the snippet below. // checking for file existence by opening the file ifstream source(fileName); if (source) ... The reason for my concern is that I am going to be reading a text document containing a list of files, and all that I need to know is if the files exist on disk or not. If there is a way to check without opening each one, that would be ideal. Thanks in Advance
Jeff wrote:
Hello,
I can see from a FileSystem example how to check for the existence of a directory. What I would like to be able to do is to check for the existence of a file but I couldn't figure out how to do it using exists().
The way that I currently check for a file's existence is by opening the file. I wonder if boost::filesystem can test for a file's existence more efficiently than having to open it like I do in the snippet below.
// checking for file existence by opening the file ifstream source(fileName); if (source) ...
What about (Boost 1.34):
#include
Johan Nilsson wrote:
Jeff wrote:
Hello,
I can see from a FileSystem example how to check for the existence of a directory. What I would like to be able to do is to check for the existence of a file but I couldn't figure out how to do it using exists().
The way that I currently check for a file's existence is by opening the file. I wonder if boost::filesystem can test for a file's existence more efficiently than having to open it like I do in the snippet below.
// checking for file existence by opening the file ifstream source(fileName); if (source) ...
What about (Boost 1.34):
#include
...
boost::filesystem::exists(fileName);
Oops, I didn't read the first part of the question. See other poster's reply. / Johan
Johan, Thank you for responding. If you or someone else can show me how to specify an explicit path, I would appreciate it. Thanks again
Hello,
On Thu, 26 Apr 2007 12:23:30 +0200, Jeff
Hello,
I can see from a FileSystem example how to check for the existence of a directory. What I would like to be able to do is to check for the existence of a file but I couldn't figure out how to do it using exists().
Simply: path filepath;// this is a path to a file if (exists(filepath) && !is_directory(filepath)) { ... the file is there } else { ...the file is not in there, or it is a directory. } Greetings, Luca
Luca Cappa
path filepath;// this is a path to a file if (exists(filepath) && !is_directory(filepath)) { ... the file is there }
Thank you. I can make it work with relative paths such as: fs::path file_path( "test2/dir1/file01.txt" ); // works fs::path file_path( "somefile.txt" ); // works I just cannot figure out now is how to specify the drive and directory: fs::path file_path( "C:/Logs/log.txt" ); // does not work I need the ability to specify the explicit path since my paths will be provided at runtime (from user input and INI files) therefore I cannot rely on paths relative to the application. Thanks in advance
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Jeff Dunlap Sent: Thursday, April 26, 2007 4:04 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users]FileSystem - Test if file exists without opening the file?
I can make it work with relative paths such as:
fs::path file_path( "test2/dir1/file01.txt" ); // works fs::path file_path( "somefile.txt" ); // works
I just cannot figure out now is how to specify the drive and directory:
fs::path file_path( "C:/Logs/log.txt" ); // does not work
[Nat] Try: fs::path file_path("c:/Logs/log.txt", fs::no_check);
participants (5)
-
Jeff
-
Jeff Dunlap
-
Johan Nilsson
-
Luca Cappa
-
Nat Goodspeed