Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check the existence of a directory

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
may i know how to check whether the directory is exist or not in the hard disc using C/C++?
 
It rather depends on the Operating System and compiler but there is usually a way of opening and reading the directory itself, for example under Linux and other Unix implementations the opendir() and closedir() functions will tell you whether the directory exists.

Under Window$ and the M$ compiler you could use the _findfirst() function or the OpenFile() function to do the same thing.

Cheers - Gavin
 
Hi:

Under Unix, trying to open a directory low-level, sets the global integer
errno to value EISDIR. There's probably fancier ways to do this under
Windows, but ....

Regards,

Ed

#include <stdio.h>

void main()
{
char *directory=&quot;/etc&quot;;

if(isdir(directory))
printf(&quot;It's a directory\n&quot;);
else
printf(&quot;It's NOT a directory\n&quot;);

exit(0);
}

/*
* return TRUE if directory else FALSE
*/

#include <sys/errno.h>

int isdir(char *dpath)
{
int ret_val;
int fd;
extern int errno;

ret_val=(fd=open(dpath, 1)) == -1 && errno == EISDIR;
if(fd != -1 && close(fd) == -1)
perror(&quot;close directory&quot;);

return ret_val;
}
 
may i know where is the header file for _findfirst() and OpenFile() and actually how to use them?
 
windows as the os and the compiler will be either Visual C++ or Turbo C++.
 
The header for _findfirst() is located in io.h (as far as I can remember under Turbo C++ the function is called findfirst() without the leading underscore.

Refer to the compiler help for details on how to use this, normally you use the _findfirst and _findnext to get a list of files matching your filespec but seeing as you only want to determine whether the directory exists you could just call _findfirst with dirname\* as the 1st argument.
 
may i know actually how to use the _findfirst() and _findnext() functions?? i really can't figure them out. any example?
 
Code:
#include <windows.h>

....

DWORD dwAttr = GetFileAttributes ( &quot;C:\\Is\\This\\Dir\\Present&quot;);

if ( dwAttr == 0xffffffff )
   { // Some error occurred, directory not present
     // Call GetLastError ( ) to find out reason
     // for failure here
     ....
   } else
   { if ( dwAttr & FILE_ATTRIBUTE_DIRECTORY )
        { // Direcory present
        } else
        { // Name present, but is not a directory
        } }
Marcel
 
Use _stat example follows:-


int result;
struct _stat buf;
result =_stat(name,&buf); // get status of file
if(result!=0){
printf(&quot;Error getting status&quot;);
return(FALSE);
}
if((buf.st_mode)&_S_IFDIR){ // if found directory, stack it.
printf(&quot;Hello I'm a directory\n&quot;);
}

name is a pointer to the file name
sorry about the missing } but it gives you an idea
 
There is a pretty simple way to do this,you can use this little function:

bool fExist( char* filename )
{
WIN32_FIND_DATA file;
HANDLE hFile;
if (( hFile = FindFirstFile( filename,&file ))
== INVALID_HANDLE_VALUE )
{
return false;
}
return true;
}

this function returns &quot;true&quot; if the directory or file exist or &quot;false&quot; otherwise.
 
with turbo c 2.0
you can
#include<dir.h> and use function
findfirst()and findnext().
press F1 to ask help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top