Results 1 to 3 of 3

Thread: Determine if File Exists [C++6]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question Determine if File Exists [C++6]

    Given a String [sPath] that is supposed to represent a Path/Name [like c:\Folder\File.txt].
    I need a way to determine if that file actually exists and return some kind of boolean value. Any ideas? Thanks,

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Determine if File Exists [C++6]

    Quote Originally Posted by Shaitan00
    Given a String [sPath] that is supposed to represent a Path/Name [like c:\Folder\File.txt].
    I need a way to determine if that file actually exists and return some kind of boolean value. Any ideas? Thanks,

    I'm not shure if this is correct and I don't know how to use Win32 API calls in C (Yet) so, here goes...

    Code:
    #include <stdio.h>
    
    FILE *fp;
    int FileExists;
    fp = fopen("SomeFile.txt", "r");
    if (fp == NULL){
        FileExists = 0;
    }
    else{
        FileExists = 1;
        fclose(fp);
    }
    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Determine if File Exists [C++6]

    It's correct and works most of the time. It's not very efficient, though, and may report false negatives if:
    1) The file exists but you're not allowed to access it.
    2) The file exists but has been locked against reading by another application.
    3) Any other reason why the file might exist but not be openable.


    Boost.Filesystem provides the exists method, and the WinAPI provides the FileExists function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width