Thread: How to check if a file exists

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    How to check if a file exists

    I only need a bool to check if file exists, while i'm giving the file location, depending on current directory.
    Last edited by ElastoManiac; 12-05-2005 at 07:04 AM.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    try to open it. If open fails then the file doesn't exist. stat() will also indicate if the file exists.

  3. #3
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    you mean with <fstream>
    i tryed that, and it doesn't work for reason unknown.
    Code:
    ifstream fin;
    fin.open("sdfsdfsdf");
    if(fin.fail) // doesn't work
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Quote Originally Posted by ElastoManiac
    you mean with <fstream>
    i tryed that, and it doesn't work for reason unknown.
    Code:
    ifstream fin;
    fin.open("sdfsdfsdf");
    if(fin.fail) // doesn't work
    Code:
    ifstream file;
    file.open("TehLeetFile");
    if(!file.is_open())
        cout<<"Tehleetness is busted!";
    else
        cout<<"it work!!!!11!!!11!one!";
    that is how I always check

  5. #5
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Wraithan
    Code:
    ifstream file;
    file.open("TehLeetFile");
    if(!file.is_open())
        cout<<"Tehleetness is busted!";
    else
        cout<<"it work!!!!11!!!11!one!";
    that is how I always check
    that worked, thanks.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if(fin.fail) // doesn't work
    You should be trying to call the fail() function, like this:
    Code:
    if(fin.fail() )

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by 7stud
    You should be trying to call the fail() function, like this:
    Code:
    if(fin.fail() )
    I was using that all the time, just that i didn't wrote it 'cause i was tired!
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I use the win32 API call FindFirstFile and determine if the file is found by the returned WIN32_FIND_DATA; check out the first tutorial(s) here: http://www.adrianxw.dk/SoftwareSite/index.html
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ahluka
    I use the win32 API call FindFirstFile and determine if the file is found by the returned WIN32_FIND_DATA;
    GetFileAttributes() doesn't require as much typing

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Note that opening the file only indicates whether you can open the file. This may be due to it not existing, but it may also be due to you not having read access to it.

    On a further note, NTFS has a weird feature called offline files that would cause serious delay if you touched a file this way. (On the other hand, that feature is practically never used.)
    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

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    if you want to use win32 API you could use PathFileExists.

    simple, but only works on drive letters (i.e. no unc paths)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. How to check if a file exists
    By ElWhapo in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2004, 05:16 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. check if the file exists
    By ipe in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 09:18 AM