Sign in to follow this  
TheC00L1

Determining if a file of directory exists

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by TheC00L1
Ummm.... sorry, let me clarify myself, open up internet explorer and having a webpage load.


The only method I know would be to use ShellExecute, passing in the URL, but there has to be a better way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this