checking for a files existance

This is a discussion on checking for a files existance within the C++ Programming forums, part of the General Programming Boards category; Whats the best way to check and see if a file exists?...

  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
    zen
    zen is offline
    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 02: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
    zen
    zen is offline
    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, 10:38 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 03:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 11:56 AM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 01:59 AM
  5. checking existance
    By LloydUzari in forum C++ Programming
    Replies: 15
    Last Post: 12-23-2004, 10:36 PM

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