Jump to content
  • Advertisement
Sign in to follow this  
demonsbreath

file exist in C++?

This topic is 5456 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

is there a command in C++ roughly equivalent to the file exist command in DarkBASIC. I need to search for .txt's where the user inputs their name and if they don't exist then I would let the user create them, and if they do I would have an error message saying they should choose a new name. Also, is there a way to list all the files in a directory in C++ (like the dir command in DOS).

Share this post


Link to post
Share on other sites
Advertisement
Hmmm...let's see here, using FILE* file = fopen(filename,"r") should work. What that ought to do is attempt to open the file in read-only mode and not create it if it doesn't exist. Then again, I'm sketchy on file operations, so that could be completely wrong (correct me if it is, please).

As for the listing all the files in a directory, no, I don't know that one. But I guess one of the big brains will come along and make everything right very shortly.

Share this post


Link to post
Share on other sites
To find out if a file exists you can, as Boku San suggests, open it in reading/no create mode and see whether it opens successfully (this will work with fstreams as well). For more involved file system operations, such as directory listings, standard C++ does not offer any functionality since it is highly platform dependant. You may find that boost::filesystem does what you need.

Share this post


Link to post
Share on other sites
Quote:
Original post by demonsbreath
is there a command in C++ roughly equivalent to the file exist command in DarkBASIC.


If you can open it, then it exists. If you can't open it, it doesn't.

Quote:
I need to search for .txt's where the user inputs their name and if they don't exist then I would let the user create them, and if they do I would have an error message saying they should choose a new name.



#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string name;
std::ofstream ofs;

while(true)
{
std::cout << "Choose a name: ";
std::cin >> name;
std::ifstream ifs(name.c_str());
if (ifs.is_open())
{
ofs.open(name.c_str());
if (ofs.is_open())
break;
}
std::cout << "This name is already taken." << std::endl;
}

// do stuff with ofs
}




Quote:
Also, is there a way to list all the files in a directory in C++ (like the dir command in DOS).


Yes, but you will have to download the Boost.Filesystem library - it will also provide the desired exists() function (using a filesystem operation, not trying to open the file). Other methods are platform-specific (opendir() or FindFirstFile() depending on your OS).

Share this post


Link to post
Share on other sites
That's a bit heavy-duty. If you're working on Windows, try the following:


#define FILE_EXISTS 0
#define FILE_WRITABLE 2
#define FILE_READABLE 4

if (_access ("somefile.txt", FILE_EXISTS) == 0)
{
//File exists
}
if (_access ("somefile.txt", FILE_READABLE) == 0)
{
//File can be read
}
if (_access ("somefile.txt", FILE_WRITABLE) == 0)
{
//File can be written
}

if (_access ("somefile.txt", FILE_READABLE|FILE_WRITABLE) == 0)
{
//File can be read and written
}

Share this post


Link to post
Share on other sites
Quote:
If you can open it, then it exists. If you can't open it, it doesn't.

Except when some other program have claimed some exclusive rights for it, like OF_SHARE_DENY_READ in Windows. But since you won't be able to read it then anyway, it's not like if it matters.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jolle
Quote:
If you can open it, then it exists. If you can't open it, it doesn't.

Except when some other program have claimed some exclusive rights for it, like OF_SHARE_DENY_READ in Windows. But since you won't be able to read it then anyway, it's not like if it matters.


In that situation, as far as you're concerned, the file doesn't exist. If you can't write to it, and you can't read from it, it's basically not there. That is, unless you need to use that filename...then what do you do?

Share this post


Link to post
Share on other sites
Thanks. I tried Kippesoeps method - it seemed the easiest to understand. Unfortunately:

Compiling...
e:\james\c__fil~1\databa~1\main.cpp
e:\james\c__fil~1\databa~1\main.cpp(29) : error C2065: '_access' : undeclared identifier
e:\james\c__fil~1\databa~1\main.cpp(29) : error C2064: term does not evaluate to a function
CL returned error code 2.
MAIN.EXE - 2 error(s), 0 warning(s)


I think this might be cos im usin a crappy old compiler - MSVC++ v1.59c (the newest version is like 6.0 or something).

I can't afford a new version, but I can't download any free compilers either - I'm using my mum's computer and she's paranoid about viruses.

I'll try the other, harder methods next...

Share this post


Link to post
Share on other sites
and just so this thread might be slightly more useful for people searching the forums.

linux has a really easy way to do this:

open(filename,O_CREAT | O_EXCL);

Guaranteed to only succeed if you created the file.

Share this post


Link to post
Share on other sites
Quote:
Original post by demonsbreath
I think this might be cos im usin a crappy old compiler - MSVC++ v1.59c (the newest version is like 6.0 or something).

Uh, no. VC++ 6.0 is old, antiquated, obsolete, and generally horrible.

Quote:

I can't afford a new version, but I can't download any free compilers either - I'm using my mum's computer and she's paranoid about viruses.

I'll try the other, harder methods next...

If you are truly using a version of VC++ vastly older than 6.0, which sounds incredible, then what your compiler accepts is probably barely recogniseable as C++.

Note that while Kippesoep's method only works in Windows, the method Fruny and I recommend (boost::filesystem) will work on any computer with the Boost libraries, on any platform.

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!