C++, checking if file exists
how would i write a code that checks if a file exists?
i need it for a loggin function in the game im making, i need it to check if it exists, and if not then say for instance, this profil does not exist or something of that sort
the files im checking for are stored in binary format, (.dat)
thanks
#include <fstream>using namespace std;ifstream thefile ( filename );if ( thefile.is_open() ){ cout << "The file exists!" << endl; thefile.close(); }else{ cout << "The File does not exits!" << endl; }
Or try out Boost::filesystem.
[Edited by - KulSeran on December 7, 2006 4:35:55 PM]
can you explain how to use that?
because i have no clue and so all its doing for me is generating errors
because i have no clue and so all its doing for me is generating errors
your probally not including the header file.
Trying putting #include <fstream> I beleive it is.
If that dosen't help, then post that it dosen't, and you might want to give us some code also.
Chad.
Trying putting #include <fstream> I beleive it is.
If that dosen't help, then post that it dosen't, and you might want to give us some code also.
Chad.
You may also need to add this to the top, below where you added the include line
using std::ifstream;
using std::ifstream;
i know how to store/recall from files, just not how to check if they exist (hence the name of the thread)
and also if i rmemeber correctly
i just need to know how to use that slab of code he suggested using
and also if i rmemeber correctly
i just need to know how to use that slab of code he suggested using
Quote:Original post by alway616
can you explain how to use that?
because i have no clue and so all its doing for me is generating errors
Post the errors.
You will need to #include <fstream>, and use "std::" in front of ifstream.
This code should compile:
#include <fstream>#include <iostream> int main(){ std::ifstream thefile ( "someFile.dat" ); if ( thefile.is_open() ) { std::cout << "The file exists!" << std::endl; } else { std::cout << "The file does not exits!" << std::endl; }}
nope i copyed and pasted it and it doesn't compile
btw my compiler is Dev C++ v 4.9.9.2
if that has to do with anything
btw my compiler is Dev C++ v 4.9.9.2
if that has to do with anything
The actual error message is usually more important than the compiler used. In this case, I suspect your problem is that it should be std::ifstream, not std::istream. std::ifstream is a std::istream, but it is more than just that. For one, it knows how to open a file, while a simple std::istream does not.
[edit]Nevermind, then[/edit]
[edit]Nevermind, then[/edit]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement