Determining if a file of directory exists
I know this might be easy for some of you, but how do you figured out wether a file of directory exists, in c++. I tried using the makedir one, mkdir("directory");, but that's just annoying cause it generates a directory.
Edit: oh yeah, also could you show me how to open up a webpage using c++?
Thanx in advance!
I think that CreateFile is either what your looking for, either completely off topic :) (as you didnt mention using win32...)
Janta
Janta
Or ...
Dunno about directories, tho.
#include <fstream>#include <iostream>#include <string>int main(){ std::string strFileName = "SomeFile.txt"; std::ifstream test( strFileName.c_str(), std::ios::in ); if ( ! test ) { // Failed to open file. Probably doesn't exist. std::cout << "Could not open file " << strFileName << "!\n"; } else { // File opened. Definitely exists. std::cout << "File " << strFileName << " opened successfully.\n"; } return 0;}
Dunno about directories, tho.
Or boost::filesystem::exists() and boost::filesystem::is_directory().
Open a socket to the webserver.
Send the appropriate HTTP request.
Retrieve the data sent by the server.
Quote:Do you also know how to open up a webpage using c++?
Open a socket to the webserver.
Send the appropriate HTTP request.
Retrieve the data sent by the server.
An alternative to Red Ant's solution is to check ifstream::fail(), but if memory serves me correctly, that function can mislead. The following algorithm describe the problem:
Open a nonexistant file with an ifstream object.
Close file.
Using the same object, open an existing file.
Check the fail() method of the ifstream object.
Note that step 4 will yield "true", indicating that the existant file does not exist.
GetFileAttributesEx() will return NULL if a file doesn't exist.
Open a nonexistant file with an ifstream object.
Close file.
Using the same object, open an existing file.
Check the fail() method of the ifstream object.
Note that step 4 will yield "true", indicating that the existant file does not exist.
GetFileAttributesEx() will return NULL if a file doesn't exist.
Quote:Original post by Anonymous Poster
An alternative to Red Ant's solution is to check ifstream::fail(), but if memory serves me correctly, that function can mislead.
Actually, you would check is_open(). Using operator!() as Red Ant did is roughly equivalent to using fail() (it also trips on badbit and eofbit).
Quote:Using the same object, open an existing file.
Yup. Clear the status flags with fstream::clear().
Quote:Original post by FrunyQuote:Original post by Anonymous Poster
Using the same object, open an existing file.
Yup. Clear the status flags with fstream::clear().
Ah, I see.
Quote:Original post by FrunyQuote:Do you also know how to open up a webpage using c++?
Open a socket to the webserver.
Send the appropriate HTTP request.
Retrieve the data sent by the server.
Ummm.... sorry, let me clarify myself, open up internet explorer and having a webpage load.
[Edited by - TheC00L1 on June 6, 2006 9:26:14 PM]
You can also use the Win32 API GetFileAttributes, which will return INVALID_FILE_ATTRIBUTES if the file/directory does not exist.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement