Jump to content
  • Advertisement
Sign in to follow this  
TheC00L1

Determining if a file of directory exists

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
I think that CreateFile is either what your looking for, either completely off topic :) (as you didnt mention using win32...)

Janta

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!