CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2000
    Location
    Philadelphia
    Posts
    221

    how to detect if a file has been already created?


    I've got a path, say c:\mypath\myfile.txt . I 'd like to find out if myfile.txt already exists in that location. How to do that?


  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: how to detect if a file has been already created?



    CString fname("test.txt");

    int exist = GetFileAttributes(fname);

    if (exist == -1)
    {
    MessageBox(fname,"file does not exist",MB_ICONEXCLAMATION);
    }
    else
    {
    MessageBox(fname,"file exists",MB_ICONEXCLAMATION);
    }





    or another method :

    CStdioFile myFile;
    CString sFileName = "c:\\test.txt";
    CFileStatus stat;
    if( !myFile.GetStatus(sFileName, stat))
    {
    MessageBox("file does not exists");
    }







  3. #3
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    Re: how to detect if a file has been already created?

    use _access(FileName,0)

    Rating isn't important...But gurus respect it and keep high

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

    Re: how to detect if a file has been already created?

    And the REAL "C++" (question was posted under "C++" category =) solution:


    #include <fstream>
    using std::ifstream;

    ifstream file("C:\\path\\filename.ext");
    if (file)
    {
    //file exists
    }
    else
    {
    //file not exists
    }






    Rate answer if it help you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    igor@soukhov.com | ICQ:57404554 | http://soukhov.com
    Best regards,
    Igor Sukhov

    www.sukhov.net

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