Missing files detection

This is a discussion on Missing files detection within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there was any function or object that could detect in a directory if a file is ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Missing files detection

    I was wondering if there was any function or object that could detect in a directory
    if a file is missing or present. I don't seem to find anything, but maybe is this possible
    to achieve only using an "fstream" object?

    I know from when I used to code in Visual Basic, that there was in VB an object,
    named like "Directory_Info" or something like that, and with that object we could
    work with the current working directory... But in C++ I can't find its equivalent...

    Any clues?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would use a combination of CreateFile() to load the file and GetLastError() to get what error is returned if there is one. To catch the error number of the "File not found" error, I would first create a small app that simply uses CreateFile() to load a file make sure it works. Once I'd know my code works, I'd simply remove that file of that directory and call GetLastError() to get that error code. Then you can use that error code to check if a file is missing. Don't forget to close the handle returned by CreateFile() if you don't plan on using it.

    Edit: Close it after calling GetLastError() because an error could happen while closing it and the error code returned by GetLastError() wouldn't be the "File not found" error.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Try open it and then close it. If it opened we know it exists.
    Code:
    using namespace std;
    
    fstream testingFile;
    testingFile.open("**The address of file here**", ios_base::in);
    if( testingFile.is_open()) testingFile.close()  //Now we know file exists.
    else  cerr<< "File not found or could not be opened"<<endl;
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Thanks, it worked with "GetLastError()". But that seem to catch every possible error,
    so if I get an error before even opening up the file, maybe it will mess everything up...

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you could use FindFirstFile() and FindNextFile() (MS-Windows) to get the list of all the files/folders in a specific folder. *nix would use opendir() and readdir(). I'm sure there are some portable boost library functions for that too.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Boost, in fact, has a simple exist() function that just returns true if the file exists. (Though it can be confused by permissions, i.e. if the file exists but is not accessible, it will return false.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by mikahell
    Thanks, it worked with "GetLastError()". But that seem to catch every possible error,
    so if I get an error before even opening up the file, maybe it will mess everything up...
    You can use SetLastError(0) before opening the file to clear the last error, I think.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You can also check the return value of CreateFile and only check GetLastError if an error actually occurred. But why use platform-specific methods when boost::filesystem::exist() is so easy?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by CornedBee
    But why use platform-specific methods when boost::filesystem::exist() is so easy?
    because he may not be using boost. I would like to but my co won't let me.

  10. #10
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    "SetLastError(0)" worked fine, but about Boost, I could use it if I wanted it, because I'm just doing all this to learn things, but I'm not yet in a company... But I'll have to take a look at it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  5. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21