check existence of file in C++
-
#include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!
-
#include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!
do a seatch for PathFileExists. Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
-
do a seatch for PathFileExists. Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
-
PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...
-
PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...
After a lot of wrong examples from googling the web, I finally find something that works: myFileName = file; inp.open(myFileName.c_str(), ifstream::in); inp.close(); if(inp.fail()) { inp.clear(ios::failbit); cout << "No File found " << myFileName.c_str() << endl; exit(5); } else { cout << "Found File" << myFileName.c_str() << endl; } Thanks, hope this might help someone else... Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?
-
PathFileExists is a windows API and I am running C++ in linux so it doesnt work as far as I know and have been trying unless I goofed up...
meixiang6 wrote:
...I am running C++ in linux...
It helps to mention this little requirement up front.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
#include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!
meixiang6 wrote:
How do I check the existence of a file in C++?
It's not C++, but you can use
_access()
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
After a lot of wrong examples from googling the web, I finally find something that works: myFileName = file; inp.open(myFileName.c_str(), ifstream::in); inp.close(); if(inp.fail()) { inp.clear(ios::failbit); cout << "No File found " << myFileName.c_str() << endl; exit(5); } else { cout << "Found File" << myFileName.c_str() << endl; } Thanks, hope this might help someone else... Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?
meixiang6 wrote:
Bye the way is this board only for Visual C++ or can someone working on Linux with C++ ask Qs?
We pretty much assume Visual C++ here, mostly even MFC is assumed to be used. Just in case you hadn't noticed the name of this board, it is MFC/C++. You can ask C++ on Unix/Linux questions here, but you must let us know of it when posting your question.
It is a crappy thing, but it's life -^ Carlo Pallini
-
#include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!
The standard way of checking file existence on *nix (it works on Windows as well) is stat or fstat[^], I believe.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
do a seatch for PathFileExists. Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
-
The standard way of checking file existence on *nix (it works on Windows as well) is stat or fstat[^], I believe.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
#include #include #include #include #include // getcwd() definition #include // MAXPATHLEN definition using namespace std; int main() { string database; cout << "Select full path of Database" << endl; cin >> database; How do I check the existence of a file in C++? In perl it would be something like: if (!database){ print "No database found\n"; exit; } I tried looking online for hours but all the suggestions didnt work so far.. Thanks in advance!