Link to home
Start Free TrialLog in
Avatar of renetaprincy
renetaprincy

asked on

Function to identify if the file already exists??

Is there any C++ function to identify if the file already exists?. What function it is?. Please give some suggestions on this.
Avatar of Axter
Axter
Flag of United States of America image

You can use stat function or access function to deterime if the file exist.

Example:
#include <sys/stat.h>
#include <io.h>
#include <direct.h>

bool FileExist(const char* FileName)
{
   struct stat my_stat;
   return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(const char* FileName)
{
   struct stat my_stat;
   if (stat(FileName, &my_stat) != 0) return false;
   return ((my_stat.st_mode & S_IFDIR) != 0);
}

ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
       
    ifstream    in ;
    in.open ( "c:\\test.txt", ifstream::in ) ;
    if ( in.good () ) {
        cout << "Exists" ;
    }    
    system ( "PAUSE" ) ;
    return 0;
}


Amit
FYI:
Opening a file to test for existence can fail on some OS if the file has write only privileges.
It will also fail if the file is locked in none-shared mode by another program.

Not only is this less reliable, but it’s also inefficient compare to using stat or access method.
Avatar of grg99
grg99

What do you want to know:

 does the file exist?  

or

 am I allowed to read the file?

  Or

 am I allowed to write the file?

or

am I allowed to rename or delete the file?


Those are all different questions with different answers.

Also note that this kind of file inquiry is only backward-looking, it doesnt tell you anything about the file inthe future, not even one nanosecond from now.




>>What do you want to know:
>> does the file exist?  
>>am I allowed to read the file?
>>am I allowed to write the file?
>>am I allowed to rename or delete the file?

If you just need to know if the file exist, that can be determine by using the stat method, regardless of the read/write attributes of the file.

The stat method will work if the file is write-only, read-only, or read-write.

And using the stat method you can also determine if the file is a directory.

Usually when you need to determine if the file exist, it's also important to determine if it's a directory.

Trying to open a directory will fail, but will not indicate that the failure is caused because the file is a directory.
I'd still be wary of using stat() to determine anything definitive.  

In many cases the only real way of telling if you can do operation xxxx on a file is to try operation xxxx.  So why bother trying something else which might not tell you the truth?

For example, stat() IIRC  won't tell you if a file is currently locked by another user, or open by another user, or in which mode.

Also on many network file systems, the file system intentionally lies:  if one program has opened a file in read/write mode, but hasnt actually written anything, other programs *might* still get an indication the file is writeable.   Novell file systems do this.  There's also issues with files that have attached access control lists.
>>In many cases the only real way of telling if you can do operation xxxx on a file is to try operation xxxx.  So why bother trying something else which might
>>not tell you the truth?

I see the point you're trying to make now.

However, it's still better to use either the stat or access function to determine if the file exist, since it's a more reliable and efficient method.
Then attempt to perform the target operation.
If the target operation fails, you have a better idea of the cause for teh failure.
Otherwise, if you blindly try to peform the operation without first accurately determining file existence, you have no idea why you have a failure.


>>Also on many network file systems, the file system intentionally lies:  if one program has opened a file in read/write mode, but hasnt actually written
>>anything, other programs *might* still get an indication the file is writeable.   Novell file systems do this.  There's also issues with files that have
>>attached access control lists.

That's a good reason to use the stat or access functions to first accurately determine file existence.
renetaprincy,
I notice you gave a B grade.
Did I fail to give you all the required information?

You have a poor grading record:
B A A B B B B B B B

Please review EE grading policy.

If you receive a full and complete answer, you should give an A grade.
If not, then you should ask for more information, and give the expert a change to give you a more detailed answer.

This is especially so, when you have a 50-150 point question.
Ok, opinions may differ, but I think some of these answers above are all wack.

If your goal is to eventually create a file, there's no better way to find out if you can than to actually create the dang file.  There's nothing stat() can tell you that gives you even a hint that you can create a file in a particular place.  You can stst() the directory, but that just tells you the permissions of the directory, that's a long way from telling you that you can sucessfully create a file there.

If your goal is to read a file, there's no better way to find out if you can than to actually OPEN the file for reading.  Again stat() just tells you part of the story, not a hint about file locks or tentative open modes ala Novell.  And there's no guarantee the file will still be there.

If your goal is to write a file, there's no better way to find out if you can than to actually OPEN the file for writing.  Again stat() just tells you part of the story, not a hint about file locks or tentative open modes ala Novell.  And there's no guarantee the file will still be there.

Same with access(), it's incomplete info, and obsolete info at that.

Just do the action, check the error codes, and be prepared for surprises.

Again, opinions in this regard do vary,  which explains the tons of installer programs that fail because they checked the wrong thing.


There are many reasons to check for file existance other then to read or write to it.

You could be just validating a process peform by another application, and file locking status may not be important.
You may have some type of logging system, in which case you don't want to over write existing log file(s).
You may need to know if a directory exist.

I've used the FileExist() function I posted in many applications that I have developed, and in each case, my goal was not to read or write to the file.
Just part of a validation logic.