Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-02-2009
Member
 
Join Date: Jan 2009
Posts: 18
C++ code to check or search if a file exists or not?

Hi,

I want a function to check or search if a certain file exists or not?

Anyone have Idea about such function in C++?

Regards,
Reply With Quote
  #2  
Old 17-02-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: C++ code to check or search if a file exists or not?

The following code is an example of how to use stat to check if a file exists. Note the comments which show you where this function could be extended if more details are required.

Code:
#include <sys/stat.h>

bool FileExists(string strFilename) {
  struct stat stFileInfo;
  bool blnReturn;
  int intStat;

  // Attempt to get the file attributes
  intStat = stat(strFilename.c_str(),&stFileInfo);
  if(intStat == 0) {
    // We were able to get the file attributes
    // so the file obviously exists.
    blnReturn = true;
  } else {
    // We were not able to get the file attributes.
    // This may mean that we don't have permission to
    // access the folder which contains this file. If you
    // need to do that level of checking, lookup the
    // return values of stat which will give you
    // more details on why stat failed.
    blnReturn = false;
  }
  
  return(blnReturn);
}
Using stat is also portable between operating systems and therefore you should be able to use this same code on Linux, Windows, Unix, Solaris, etc. The only thing that may differ between operating systems and compilers is the name of the include file (even though it shouldn't change).
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 17-02-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: C++ code to check or search if a file exists or not?

The best bet is just open the file and ask if the file is _really_ opened. With streams you can do just this:

Code:
fstream myfile;
myfile.open(<path_and_name>, ios::read);
if (myfile.is_open())
     //the file is opened
else
     //the file was not opened --> something happens!
myfile.close();
Later you can use the return value of is_open(), to try find the cause of the error. For example, if the drive was a:\, you can ask if there is a diskette in the drive.
Reply With Quote
  #4  
Old 17-02-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: C++ code to check or search if a file exists or not?

Use FileExists function. If file exists, then this function will return True.

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
if FileExists(Edit1.Text) then
ShowMessage('File already exists!')
else
ShowMessage('This file is unique');
end;
Reply With Quote
  #5  
Old 17-02-2009
Member
 
Join Date: May 2008
Posts: 115
Re: C++ code to check or search if a file exists or not?

just open the file for reading, if this succeeds, then the file exists, something like this:

Code:
FILE* fp = fopen(path, "r");
if (fp) {
    // file exists
    fclose(fp);
} else {
    // file doesn't exist
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C++ code to check or search if a file exists or not?"
Thread Thread Starter Forum Replies Last Post
Unix: Check if a file exists with certain prefix Gracious Software Development 4 29-05-2010 06:01 PM
Check if URL exists (Java) Kelvin Little Software Development 7 08-01-2010 04:04 PM
How to check if file exists in directory with Php Zool Software Development 3 03-11-2009 12:36 PM
How to check if variable exists in C# Hamlet Software Development 3 28-08-2009 07:30 PM
How to Check File Exists or Not in Linux Ettan Software Development 0 19-12-2008 01:42 PM


All times are GMT +5.5. The time now is 01:31 PM.