Thread: checking for a files existance

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    checking for a files existance

    Whats the best way to check and see if a file exists?

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Try and open it. As long as you use the standard fstream header trying to open a file using an ifstream object that doesn't exist will fail.
    Last edited by zen; 11-13-2001 at 01:07 PM.
    zen

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    ifile ifstream;

    ifile.open("filename.ext");

    When i do that it creates the file, weather it exists or not.

    And the ifstream:pen() function dosent return a value, so how do i know if it failed?

    I was thinking i might could use the eof member somehow?


    I tried

    if(ifile.eof) createfile();

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Are you sure you're using the standard header files (the ones without the h extension)? Try this -

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
    	ifstream in;
    	in.open("doesnotexist.txt");
    
    	if(!in.is_open())
    		cout << "File doesn't exist"<<endl;
    
    
    
    	return 0;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  5. checking existance
    By LloydUzari in forum C++ Programming
    Replies: 15
    Last Post: 12-23-2004, 09:36 PM