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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Streams

Status
Not open for further replies.

benaam

Programmer
Jan 11, 2001
20
US
HI
How do i check in C++ whether a directory exists or not?
If exists does it have write permissions?
How do I do the above checks? Some sample code will helpful

Thanx in Advance
 
Which compiler are you using? BCB has a library called FileCtrl which has a function called DirectoryExists. I assume Borland C++ 5.5 has it, too.
James P. Cottingham
 
See if you have the io.h file in your libraries. In this is a function called access. It take the form of int access(const char *filename, intamode). Amode can be one of the following:
06 - Check for read and write permission
04 - Check for read permission
02 - Check for write permission
01 - (ignored but supposed to be for execute)
00 - Check for existence of file.

If the filename is a directory name, access simply returns whether the directory exists or not.

The integer returned can be 0 if the requested amode is true, or -1 for false. The following example comes from my Borland C++ 5.02 help file.
Code:
 #include <stdio.h>
 #include <io.h>

 int file_exists(char *filename);

 int main(void)
 {
    printf(&quot;Does NOTEXIST.FIL exist: %s\n&quot;,
      file_exists(&quot;NOTEXISTS.FIL&quot;) ? &quot;YES&quot; : &quot;NO&quot;);
    return 0;
 }

 int file_exists(char *filename)
 {
   return (access(filename, 0) == 0);
 }

Sorry it took so long.
James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top