i have a directory at C:\helo\one.txt
how can i check if this file exists in the directory?
Printable View
i have a directory at C:\helo\one.txt
how can i check if this file exists in the directory?
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
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?
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>
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
}
}