Easy 50 Points

ReneJ
ReneJ used Ask the Experts™
on
Hello All,

I'm new to C++ and trying to write a program that will check to see if a directory exist and if it does create a file in that directory. I know how to create the file but I don't know how to check if a directory exist.

Thanks for the help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can use stat to check if directory exist.
Another method is to use access.

But the stat method is more poratble.
Commented:
Example wrapper functions:

#include <sys/stat.h>
#include <io.h>

bool FileExist(const char* FileName)
{
    struct stat my_stat;
    return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(const char* FileName)
{
    struct stat my_stat;
    if (stat(FileName, &my_stat) != 0) return false;
    return ((my_stat.st_mode & S_IFDIR) != 0);
}

bool FileExistViaAccess(const char* FileName)
{
    return (access(FileName, 0) == 0);
}

Commented:
The stat method works on most platforms, to include UNIX, Windows, DOS, and Linux.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Commented:
In windows, use the FindFirstFile() method to check for files or directories.

IMPORTANT: don't forget to close the find handle with FindClose().

EXAMPLE:

#include <iostream.h>
#include <windows.h>

int main()
{
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
 hFind = FindFirstFile( "c:\\temp", &FindFileData );

 if ( INVALID_HANDLE_VALUE == hFind )
  cout << "Not found: " << GetLastError() << endl;
 else {
  cout << "Found file or dir." << endl;
  FindClose( hFind );
 }

  return 0;
}

Commented:
>>In windows, use the FindFirstFile() method to check for
>>files or directories.
The FindFirstFile method is overkill for checking existance.

A better windows method is to use stat or access.

Commented:
Continue....
Using the above FindFirstFile method, will not tell you if the path is a directory or a file.

Using the stat method you can make that determination.
Example:

void SomeFunction()
{
 if (IsDirectory("c:\\temp"))
   cout << "Found dir." << endl;
 else
   cout << "Not found: " << GetLastError() << endl;

Commented:
In windows, use the FindFirstFile() method to check for files or directories.

IMPORTANT: don't forget to close the find handle with FindClose().

EXAMPLE:

#include <iostream.h>
#include <windows.h>

int main()
{
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
 hFind = FindFirstFile( "c:\\temp", &FindFileData );

 if ( INVALID_HANDLE_VALUE == hFind )
  cout << "Not found: " << GetLastError() << endl;
 else {
  cout << "Found file or dir." << endl;
  FindClose( hFind );
 }

  return 0;
}

Commented:
Sorry for the double post... I hit refresh by accident.

Commented:
gladlock,
Please don't hit the refresh button when view updates to a post.
That will cause you to double post as above.
Instead, try clicking the "Reload This Question" link located in upper left corner of this page, (right under your user name).

That will avoid the double post.

Commented:
>>Sorry for the double post... I hit refresh by accident.
I posted previous message before getting your last post.
in dos you could use findfirst() and findnext()



#include <dos.h>

int Search(char dirname[])
{

  struct ffblk file;

  if(!findfirst(dirname,&file,FA_DIREC))
       return 1;

  return 0;  
}


findnext(&file) could be used to continue the search if a pattern is specified instead of a directory name...

then the code would look like...

int Search(char pattern[])
{

  struct ffblk file;

  if(!findfirst(dirname,&file,FA_DIREC))
  {
     do
     {
        printf("\ndir: %s",file.ff_fname);
     }while(!findnext(&file));
  }
  return 0;  
}


both these functions return 0 if success...

hey another point AXTERS solution is best although!

i am sorry about the previous post there is some problem in the 2 code part... it should be as follows

int Search(char pattern[])
{

 struct ffblk file;

 /* previously this was dirname */
 if(!findfirst(pattern,&file,FA_DIREC))
 {
    do
    {
       printf("\ndir: %s",file.ff_fname);
    }while(!findnext(&file));
 }
 return 0;  
}

hope that helps
cryptosid

Author

Commented:
Sorry for the delay on responding back.

Thank you all for the help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial