Archived

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

Checking if a file exits already

This topic is 5653 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 simple way to determine if a file already exits(without using win32 specific API calls). Is there perhaps a combination of flags that can be used with fstreams open method which wont create the file if it exists, so that a call to is_open will reveal if the file already existed? (I know i could just test it myself, but if someone already knows the answer... well

Share this post


Link to post
Share on other sites
Try _open from <io.h> with _O_RDONLY and check errno for ENOENT (I think this will work).

Keep in mind that if open fails, it may be not because a file doesn't exist, but because you don't have permissions, or because it's already open for writing by another program.

[edited by - IndirectX on June 23, 2002 2:38:24 PM]

Share this post


Link to post
Share on other sites
I think that method might work, using an extra flag, i looked at
http://www.digitalmars.com/rtl/io.html#_open (after reading you reply)

And it mentions this flag:
_O_EXCL - Used with O_CREAT, returns an error if file exists

I can probably use that.

[edited by - ziphnor on June 23, 2002 3:06:58 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use std::ifstream to open a file for input the then check if there was an error opening it. If there was, you could assume that it didn''t exist (technically, all you know is that you couldn''t open it which could mean a number of things). If you want anything more specific, you''ll have to use OS dependant calls.

Share this post


Link to post
Share on other sites
It seems really simple to me, run fopen and pass in the desired file name, if it will open it then it obiously already exists. If not then the opposite is true.


FILE *saveFile;

char fileName[15] = "whatever.svg";

saveFile = fopen(fileName, "wb");


if (saveFile == NULL) {

// Yeah, it''s not here so therefore there is no file with that name!

}

else {

// Okay there is a file with that name, what do you want to do?

}

Share this post


Link to post
Share on other sites
Well, there could be other reasons for the failure to open the file, and i would like to be able to differentiate between failure because it didnt exists and failure for other reasons(ie, "real" error).

The other reason i just dont try to open it, is that the most open file methods seems create the file if it doesnt exist(and then i would have to check file size to see if it had been created previously).

But the ifstream idea seems good, i guess the ifstream open method wouldnt create the file(that would be kind of silly, wouldnt it?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can also use C++ streams if you use the ios::nocreate flag.

Share this post


Link to post
Share on other sites
quote:
ios::nocreate is nonstandard


Damn it, "they" just really dont want you test for the existence of file i guess

Just how non-standard is it?(I mean, is it like hash_map which isnt standard, but which all STL libs seems to have).

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
Well, there could be other reasons for the failure to open the file, and i would like to be able to differentiate between failure because it didnt exists and failure for other reasons(ie, "real" error).



Like all streams (at least all that I know of), fstreams have methods like bad() and fail() to check for read/write errors.

quote:

The other reason i just dont try to open it, is that the most open file methods seems create the file if it doesnt exist(and then i would have to check file size to see if it had been created previously).

But the ifstream idea seems good, i guess the ifstream open method wouldnt create the file(that would be kind of silly, wouldnt it?)


You''re right. If you try to open a non-exsistent file with ifstream, it will not create the file.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
But isnt the _access function win32 only? (the game in question is target towards windows but i see no reason to rely on the win32 API if i can use standard C++ calls instead).

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
i see no reason to rely on the win32 API if i can use standard C++ calls instead

There is no Standard C++ means of telling if a file exists.

Share this post


Link to post
Share on other sites