Results 1 to 5 of 5

Thread: C++ code to check or search if a file exists or not?

  1. #1
    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,

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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).

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    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;

  5. #5
    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
    }

Similar Threads

  1. Unix: Check if a file exists with certain prefix
    By Gracious in forum Software Development
    Replies: 4
    Last Post: 29-05-2010, 06:01 PM
  2. Check if URL exists (Java)
    By Kelvin Little in forum Software Development
    Replies: 7
    Last Post: 08-01-2010, 04:04 PM
  3. How to check if file exists in directory with Php
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:36 PM
  4. How to check if variable exists in C#
    By Hamlet in forum Software Development
    Replies: 3
    Last Post: 28-08-2009, 07:30 PM
  5. How to Check File Exists or Not in Linux
    By Ettan in forum Software Development
    Replies: 0
    Last Post: 19-12-2008, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,919,660.17336 seconds with 16 queries