Determining if a file of directory exists

Started by
12 comments, last by TheC00L1 17 years, 11 months ago
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!
A wiseman once said nothing, but no one was there to hear it.
Advertisement
I think that CreateFile is either what your looking for, either completely off topic :) (as you didnt mention using win32...)

Janta
Or ...


#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.
Thanx, i'll try those! Yeah it is win32.

Do you also know how to open up a webpage using c++?
A wiseman once said nothing, but no one was there to hear it.
Or boost::filesystem::exists() and boost::filesystem::is_directory().

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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote: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 Fruny
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.


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]
A wiseman once said nothing, but no one was there to hear it.
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