Thread: Check for file existance...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Check for file existance...

    Hey guys, Just need a quick little bit of help. How do you check if a file exists or not? I tried this...

    Code:
    ...
    if(ifstream ("filename")==0)
    {
         cout<<"File Exists!";
    }
    ...
    it didn't work.

    Thanks guys

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I believe it works like this:
    Code:
    ifstream myFile("Something.txt");
    
    if (!myFile){
    cout << "\n Failed to open file.";}
    
    else{
    cout << "\n Success.";}

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you can use is_open().
    Code:
    ifstream myFile("filename");
    if(myFile.is_open())
    {
         cout<<"File Exists!";
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or the exists() call from the CRT, which doesn't care if you actually have read permission on the file.
    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

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    or if you are using windows you can use PathFileExists()

  6. #6
    Banned
    Join Date
    Oct 2004
    Posts
    250
    I had some problems using PathFileExists();
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    int main()
    {
    	if (PathFileExists("Data.txt") == 0)
    	{
    		cout << "File Found "<<endl;
    	}
    	else
    	{
    		cout << "File Not Found " <<endl;
    	}
    	cin.get();
    }
    What header file is this declared in?

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Everything you ever wanted to know about PathFileExists, but were afraid to ask.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ken Fitlike
    Everything you ever wanted to know about PathFileExists, but were afraid to ask.
    What you meant to say was:

    Everything you ever wanted to know about PathFileExists, but were too ........ing lazy to search for!

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Code:
    #include <iostream>
    #include <windows.h>
    #include <shlwapi.h>
    
    #pragma comment(lib,"shlwapi.lib")
    
    using namespace std;
    
    int main()
    {
    	int A = PathFileExists("data.txt");
    
        if (A == 1)
        {
            cout << "File Found "<<endl;
        }
        else
        {
            cout << "File Not Found " <<endl;
        }
        cin.get();
    	return 0;
    }

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Careful, I believe
    #pragma comment(lib,"shlwapi.lib")
    is compiler specific and will not work in all cases.

    *edit - oops didn't mean to bump the thread. I didn't realize it was so old. *
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Thread wasn't old.

    This pragma is indeed compiler-specific. But then, PathFileExists is itself platform-specific.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM