Checking if a file exits already
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
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]
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]
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]
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]
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.
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?
}
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?)
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?)
Thanks AP, that should solve the problem, wonder why i hadnt noticed that flag, perhaps i just looked at a sucky reference page...
quote:Original post by Anonymous Poster
You can also use C++ streams if you use the ios::nocreate flag.
ios::nocreate is nonstandard. Neither is ios::noreplace (so you wont find them in <fstream>; they''re from <fstream.h>).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement