Alon_T
Alon_T

Reputation: 1440

a better way than stat() to look for files in dir?

I'm trying to find out whether certain files are in a certain folder. However, even though the files exist, the way I try to find them doesn't work in certain folders.

bool FileExists(string strFilename) {
  struct stat stFileInfo;
  bool blnReturn;
  int intStat;
  intStat = stat(strFilename.c_str(),&stFileInfo);
  if(intStat == 0) {
    // We were able to get the file attributes
    // so the file obviously exists.
    blnReturn = true;
    printf("Found file %s\n", strFilename.c_str());
  } else {
    blnReturn = false;
    printf("Didn't find file %s\n", strFilename.c_str());
  }

  return(blnReturn);
}

When I mount a dir in /mnt/ram .. it doesn't (and sometimes does ) find the files there, however when I use another directory which is on the disk, it always finds the files.

Is there any other way to find out whether files exist in the directory?

Thanks

Upvotes: 2

Views: 186

Answers (1)

rubenvb
rubenvb

Reputation: 76579

The result of a stat call or any other directory/file listing depends on permissions of the calling process. /mnt/ram might well be hidden for the current user.

As mentioned in the comments, opendir and readdir are the idiomatic way to get a (recursive) directory listing. Obviously, stat is part of the idiom :-).

Upvotes: 1

Related Questions