best practise to work with path

I get path from command line parameter where path could be written in 4 different ways

testdir
./testdir
c:/my path/testdir

I would like to know how to prepare the path before I can test it. I just tried
path = "./testdir" and I got error that the directory does not exist. But it does. So how should I pass the path? Do I need complete name of path and how to get it?

I am getting the path like this
char * workingPath = _getcwd( NULL, 0);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool MyClass::IsDirectory(std::string path){
struct stat s;
path = "./" + path;
const char * cpath = path.c_str();
if( stat(cpath,&s) == 0 )
{
    if( s.st_mode & S_IFDIR )	// it's a directory
    {
	  cout << cpath << ": directory" << "\n" ;
	  MyClass::readDirectoryFiles = true;
      return true;
    }
    else if( s.st_mode & S_IFREG )  // it's a file
    { 
	  cout << cpath << ": file" << "\n" ;
	  MyClass::readDirectoryFiles = false;
      return false;
    }
    else
    { cout << cpath << ": Not file or directory" << "\n" ;
		return false;
        //something else
    }
}
else
{
	cout << cpath << ": Path not found" << "\n" ;
	_getch();
    //error
	exit(-1);
}
}
Last edited on
Isn't this platform dependent? ./testdir should work on Linux, but I don't think that's a path definition on Windows. c:/my path/testdir should work on windows but probably not on Linux since it needs either a relative path definition (relative to where you run the program from) or an absolute path definition which on Linux I think would not start from c.

So if you work on Windows, I think "./testdir" wouldn't work on Windows and might not work on Linux if you're not in the parent directory of testdir. Which package/module (sorry don't know exactly what the right term is for C++) does the method _getcwd belong to? Probably the same package contains a method for getting the absolute path. I would recommend getting the absolute path if possible.
Topic archived. No new replies allowed.