CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: file exists

  1. #1
    Join Date
    May 2000
    Location
    Ireland
    Posts
    503

    file exists

    to determin if a file exists !
    do i have to try and open the file and see if it is successfully opened for read! or is there a command to do it?

    cheers
    b

    Of course I do it for the ratings!

  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: file exists

    Hi ! Hope this function will usefull for you.


    #include <fstream>

    bool FileExists (const char *fname)
    {
    return std::ifstream(fname) != NULL;
    }







    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Member of Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: file exists

    You don't have to open it. You can use the stat function.

    Example:
    #include <sys/types.h>
    #include <sys/stat.h>

    struct stat f__stat;
    bool FileExist = (stat(lpFileName,&f__stat) != 0)

    Although stat() is a non-Ansi C++ function, most compilers support it.

    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

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

Featured