Check that file is on disk?

M

mlt

Is there some function where I can check that a path to file is correct
(that the file exists)?
 
E

Erik Wikström

Is there some function where I can check that a path to file is correct
(that the file exists)?


There is not standard C++ function for this, you will have to use a
platform specific function. Try asking an a group discussing programming
on your platform.
 
B

Bo Persson

mlt said:
Ok so no hints for this on windows Vista 64 bit?

This is not Vista specific, but the general hint is to try to open the
file, and see if that succeeds.

Most other ways of checking for files might fail in every way
possible, including that some other program might create (or delete)
the file moments after you checked for its existance. On some systems
you might be able to read or write a file, but you might not be
allowed to scan a directory. etc.


Bo Persson
 
J

James Kanze

On 2009-02-01 21:17, mlt wrote:
There is not standard C++ function for this, you will have to
use a platform specific function. Try asking an a group
discussing programming on your platform.

It's probably worth adding that on most of the usual systems,
there is a race condition involved as well. Depending on the
application, it may not be important, but it is present. And of
course, in the absolute, it is impossible on most systems;
again, whether this is relevant depends on the application.
 
I

Ian Collins

Andy said:
I could give you several ways, all of which are clumsy, and none of them
Vista64 specific (work on all windows)

You could always use fopen - but that's C, not C++, and off-topic for
this group.
Says who? fopen() is declared in <cstdio>.
 
W

WANG Cong

Andy said:
I could give you several ways, all of which are clumsy, and none of them
Vista64 specific (work on all windows)

You could always use fopen - but that's C, not C++, and off-topic for
this group.

How about ifstream.open()?
 
M

mlt

It seems that:

String test = "../../test/test.txt"
if ( !boost::filesystem::exists( test )) {
std::cout << "Script file NOT found!\n";
}

only works if I specify the whole path to test.txt and not just the relative
path. Is there some way to make the call work for relative paths?
 
N

Noah Roberts

Erik said:
There is not standard C++ function for this, you will have to use a
platform specific function. Try asking an a group discussing programming
on your platform.
TR2.
 
J

Jorgen Grahn

It seems that:

String test = "../../test/test.txt"

(Syntax error, and what's a String -- some Boost thing?)
if ( !boost::filesystem::exists( test )) {
std::cout << "Script file NOT found!\n";
}

only works if I specify the whole path to test.txt and not just the relative
path. Is there some way to make the call work for relative paths?

I do not know boost::filesystem, but that must be a user error. Such
tests would be useless if they didn't support relative paths (on OSes
which have relative paths, of course).

(And the Perl programmers would laugh at us.)

/Jorgen
 
P

Pascal J. Bourguignon

Erik Wikström said:
There is not standard C++ function for this, you will have to use a
platform specific function. Try asking an a group discussing programming
on your platform.

That would depend what you consider standard C++. Is the stl part of standard C++?

If you accept the stl as part of standard C++, then:


#include <ciso646>
#include <string>
#include <istream>

class File {
protected:
std::string path;
public:
File(const std::string& aPath):path(aPath){}
boolean exists(){
std::ifstream ifs(path,ifstream::in);
return(not ifs.fail());
}
}

File("/some/path").exists()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top