-
October 19th, 2003, 10:44 AM
#1
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?
-
October 19th, 2003, 10:51 AM
#2
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
-
October 19th, 2003, 12:20 PM
#3
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?
-
October 19th, 2003, 12:23 PM
#4
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>
-
October 20th, 2003, 12:29 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|