CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    82

    how i can check if a file exists in a specific directory?

    i have a directory at C:\helo\one.txt
    how can i check if this file exists in the directory?

  2. #2
    Join Date
    Dec 2002
    Posts
    47
    You can use this CRT function ...

    #include <io.h>

    int _access(const char *path, int mode);

    00 Existence only
    02 Write permission
    04 Read permission
    06 Read and write permission

    A zero return means the file is accessible (exists).

    -rick

  3. #3
    Join Date
    Oct 2003
    Posts
    82
    i use it like this

    #include <io.h>
    #include <stdio.h>
    #include <stdlib.h>

    if ( (_access("C:/one.txt",0))== -1)
    {
    MessageBox("File Does Not Exists");
    }

    but i have an error saying

    '_access' undeclared indentifier

    what is wrong?

  4. #4
    Join Date
    Oct 2003
    Posts
    82
    its ok nothing is wrong i just had to put the include files after the

    #include "stdafx.h"

    like this
    #include "stdafx.h"
    #include <io.h>
    #include <stdio.h>
    #include <stdlib.h>

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Code:
    CString szPath("c:\\windows");
    
    DWORD dwAttr = GetFileAttributes(szPath);
    if (dwAttr == 0xffffffff)
    {
      DWORD dwError = GetLastError();
      if (dwError == ERROR_FILE_NOT_FOUND)
      {
        // file not found
      }
      else if (dwError == ERROR_PATH_NOT_FOUND)
      {
        // path not found
      }
      else if (dwError == ERROR_ACCESS_DENIED)
      {
        // file or directory exists, but access is denied
      }
      else
      {
        // some other error has occured
      }
    }
    else
    {
      if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
      {
        // this is a directory
      }
      else
      {
        // this is an ordinary file
      }
    }

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