2

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

Olivier
  • 1,981
  • 7
  • 24
  • 29

2 Answers2

2

Be aware that /path/to/folder is different from /path/to/folder/

Marius
  • 90
  • 6
2

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"
Kornel
  • 5,264
  • 2
  • 21
  • 28
  • dir is an std::string passed as argument from the main program. Manually inputting ./program folder/foldername works, but when I use a script as such : script.sh folder/ foldername, which calls ./program $folder$foldername, it does not work yet it is the same string. – Olivier Feb 12 '15 at 08:45
  • Do you run the shell script from the same folder as `program` is? I think it could be a mismatch between relative and absolute paths. – Kornel Feb 12 '15 at 09:00
  • me again, what does `ls -l $folder$foldername` result in `script.sh`? – Kornel Feb 12 '15 at 09:01
  • It results in "No such file or directory", however, when outputting the string in my C++ program right before opendir(), the strings are the same. I specify the absolute path (I tried both) – Olivier Feb 12 '15 at 09:04
  • ls $folder works, but ls $folder$foldername does not work in the script. Doing ls for the foldername I want works in the terminal. How do get this to work in my script? It seems like concatenating the strings is causing the issue, though this seems to be the recommended way to do it – Olivier Feb 12 '15 at 09:07
  • http://stackoverflow.com/questions/4181703/how-can-i-concatenate-string-variables-in-bash – Kornel Feb 12 '15 at 09:13
  • I have tried them all. ls to whatever string I concatenate gives me the same error – Olivier Feb 12 '15 at 09:20
  • First line prints the path correctly, second line "No such file or directory". When I ls in terminal the first line's path, it works...I really don't know what is going on – Olivier Feb 12 '15 at 09:33
  • Definitely just realized that I hadn't fixed something in my code, so the directory I was trying to get C++ to access didn't exist at the time... *facepalm* – Tanner Strunk Mar 15 '18 at 16:10