Archived

This topic is now archived and is closed to further replies.

PixolMaster

How do I find out if a file exists?

Recommended Posts

PixolMaster    302
Hi, I''m writing a program that saves level-files, and what I want it to do is increment the files. save0.lev save1.lev save2.lev ... ... So... what I need to do is find out if a file exists, and if one does I increment the number and try again. I''m making a win32 program. I tried searching google, and all I found was that it''s platform specific... I also tried searching the gdnet forums, but got an error in doing so. Any help would be greatly appreciated! Thanks ahead of time. --- Alex Broadwin --- Domini Nocti Games

Share this post


Link to post
Share on other sites
llyod    122
How about just trying to open it with fopen() or the equivalent in C++? If it opens, then it''s there, and you can close it again.

Share this post


Link to post
Share on other sites
Interim    122
You can use the member function of fstream is_open() to check if a file exists.



fstream f;

f.open("myFile.txt");

if (f.is_open()) {
cout << "File is legit." << endl;
}
else {
cout << "File not found." << endl;
}

f.close();


Interim.

[edited by - Interim on August 12, 2003 5:43:18 PM]

Share this post


Link to post
Share on other sites
PixolMaster    302
quote:
Original post by Interim
You can use the member function of fstream is_open() to check if a file exists.



fstream f;

f.open("myFile.txt");

if (f.is_open()) {
cout << "File is legit." << endl;
}
else {
cout << "File not found." << endl;
}

f.close();


Interim.

[edited by - Interim on August 12, 2003 5:43:18 PM]


Hmm, also very good to know! Thank you very much. :~)


--- Alex Broadwin
--- Domini Nocti Games

Share this post


Link to post
Share on other sites
Kestrel    158
This might work better.

#include <io.h>
int access(const char *filename, int amode);
int _waccess(const wchar_t *filename, int amode);

Description

Determines accessibility of a file.

access checks the file named by filename to determine if it exists, and whether it can be read, written to, or executed.

The list of amode values is as follows:

06 Check for read and write permission
04 Check for read permission
02 Check for write permission
01 Execute (ignored)
00 Check for existence of file

Under DOS, OS/2, and Windows (16- and 32-bit) all existing files have read access (amode equals 04), so 00 and 04 give the same result. Similarly, amode values of 06 and 02 are equivalent because under DOS write access implies read access.

If filename refers to a directory, access simply determines whether the directory exists.

Return Value

If the requested access is allowed, access returns 0; otherwise, it returns a value of -1, and the global variable errno is set to one of the following values:

ENOENT Path or file name not found
EACCES Permission denied

Share this post


Link to post
Share on other sites