Error opening existing directory using opendir in c++
I am making use of opendir() as below to access a directory.
DIR *dp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
However, I keep getting the error below, even though the directory exists.
Error(2) opening /path/to/folder/
I am able to get a list of file names when I do ls /path/to/folder
请注意,/ path / to /文件夹与/ path / to / folder /
errno
value 2 means ENOENT
(it's an abbreviaton for Error NO ENTry) that is "Directory does not exist, or name is an empty string". How do you define dir
in your code?
std::string dir = "/path/to/folder/";
DIR* dp = opendir(dir.c_str());
if (dp == NULL)
{
std::cout << "Error(" << errno << ") opening " << dir << std::endl;
perror("opendir");
return errno;
}
closedir(dp);
Update #1:
Try to call you shell script:
main.sh folder/ foldername
Where main.sh
contains:
#!/bin/sh
path="$1$2"
echo "$path"
ls -l "$path"
链接地址: http://www.djcxy.com/p/29146.html