4

I've got a pretty basic console program here, to determine if a folder or file exists or not using stat:

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

using namespace std;

int main() {
  char path[] = "myfolder/";
  struct stat status;

  if(stat(path,&status)==0) { cout << "Folder found." << endl; }
  else { cout << "Can't find folder." << endl; } //Doesn't exist

  cin.get();
  return 0;
}

I have also tried the access version:

#include <iostream>
#include <io.h>

using namespace std;

int main() {
  char path[] = "myfolder/";

  if(access(path,0)==0) { cout << "Folder found." << endl; }
  else { cout << "Can't find folder." << endl; } //Doesn't exist

  cin.get();
  return 0;
}

Neither of them find my folder (which is right there in the same directory as the program). These worked on my last compiler (the default one with DevCpp). I switched to CodeBlocks and am compiling with Gnu GCC now, if that helps. I'm sure it's a quick fix - can someone help out?

(Obviously I'm a noob at this so if you need any other information I've left out please let me know).

UPDATE

The problem was with the base directory. The updated, working program is as follows:

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

using namespace std;

int main() {
  cout << "Current directory: " << system("cd") << endl;

  char path[] = "./bin/Release/myfolder";
  struct stat status;

  if(stat(path,&status)==0) { cout << "Directory found." << endl; }
  else { cout << "Can't find directory." << endl; } //Doesn't exist

  cin.get();
  return 0;
}

ANOTHER UPDATE

Turns out that a trailing backslash on the path is big trouble.

Ben
  • 54,723
  • 49
  • 178
  • 224
  • Call perror(NULL) after the failure, it might give you a clue. Also, I think you need to pass flags to access() instead of 0, but I'm not sure why stat is failing. – ergosys Dec 14 '10 at 01:33
  • It worked when I compiled with DevCpp :/ so I think the syntax is OK... – Ben Dec 14 '10 at 01:36
  • Putting the C tag back in since, despite the use of iostream, the crux of the question relates to C quite well. – paxdiablo Dec 14 '10 at 02:02
  • 1
    It's probably because they are called directories, not folders ;-) – user502515 Dec 14 '10 at 02:14
  • Fair enough, friend @user502515. Terminology is important :) – Ben Dec 14 '10 at 02:42

4 Answers4

5

Right before your stat call, insert the code:

system("pwd");  // for UNIXy systems
system("cd");   // for Windowsy systems

(or equivalent) to check your current directory. I think you'll find it's not what you think.

Alternatively, run the executable from the command line where you know what directory you're in. IDEs will frequently run your executable from a directory you may not expect.

Or, use the full path name so that it doesn't matter which directory you're in.

For what it's worth, your first code segment works perfectly (gcc under Ubuntu 10):

pax$ ls my*
ls: cannot access my*: No such file or directory

pax$ ./qq
Cannot find folder.

pax$ mkdir myfolder

pax$ ll -d my*
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder/

pax$ ./qq
Folder found.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Running from CMD worked. `system("pwd")` didn't. But anyway I'm on the right track again. Thanks for the help. – Ben Dec 14 '10 at 01:37
  • @Steve, apologies for that, I didn't realise you were running under Windows. Adding that variant for completeness. – paxdiablo Dec 14 '10 at 01:46
  • Turns out another thing throwing this off was the trailing backslash at the end of my path. – Ben Dec 14 '10 at 02:46
1

Are you sure that the current directory of your running program is what you expect it to be? Try changing path to an absolute pathname to see if that helps.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

Check your PWD when you running your program. This problem is not caused by compiler. You DevCpp may set a working directory for your program automatically.

Huang F. Lei
  • 1,835
  • 15
  • 23
0

You can find out why stat() failed (which is a C function, not C++, by the way), by checking errno:

#include <cerrno>

...

if (stat(path,&status) != 0)
{
    std::cout << "stat() failed:" << std::strerror(errno) << endl;
}
Alex B
  • 82,554
  • 44
  • 203
  • 280
  • Actually, my title and tags were edited to change C to C++ :/ – Ben Dec 14 '10 at 01:53
  • `stat()` isn't actually a C function either (as in, it's not defined by C99) but, if it was, it would almost certainly be a C++ one as well :-) I suspect the tags were changed simply because of the `cout`-type stuff but the question is as much for C as C++. – paxdiablo Dec 14 '10 at 02:00
  • @paxdiablo, well, it's from POSIX C API, so it's C in my book :) – Alex B Dec 14 '10 at 02:49