1

I am trying open dir and read files inside it .

but the function opendir() returns always "No such file or directory", and when I put the the path it's working .

Working:

if (!(dir = opendir("/home/user/workspace/OS-Ex2Files/students/"))){
    perror(dir);
    return;
}

Not working:

printf(dirPath); // prints  /home/user/workspace/OS-Ex2Files/students/
if (!(dir = opendir(dirPath))){
    perror(dir);
    return;
}

dirPath is a char* .

Can you please explain me why the second option is not working?

Aviv Eyal
  • 307
  • 2
  • 3
  • 13
  • 5
    Are you sure there's no superfluous whitespace in `dirPath`? Can you try checking with `strcmp` to be sure? – Oliver Charlesworth Dec 06 '17 at 19:30
  • srtcmp returned that the strings are not equals , but not because superfluous whitespace. @user3121023 - if was filled using getline (from a FILE) - and you answer solved the problem! now its working – Aviv Eyal Dec 06 '17 at 19:38
  • 1
    Well, then you have found your issue :) – Oliver Charlesworth Dec 06 '17 at 19:39
  • 1
    If `printf(dirPath);` prints a newline, then that's the problem. You shouldn't use that; you should write `printf("dirPath = [%s]\n",dirPath);` which is both safer (file names containing `%` would send `printf()` into a tizzy) and makes it easier to see embedded newlines or carriage returns because of the `[` and `]` markers surrounding the string. – Jonathan Leffler Dec 06 '17 at 19:42
  • 1
    FIY in C the newline character `\n` is considered a whitespace. – Eugene Sh. Dec 06 '17 at 19:57
  • I'm not having any problems in my own implementation; can you show us how you're setting the `dirPath` variable? – Jose Fernando Lopez Fernandez Dec 06 '17 at 20:21
  • @Jose - dirPath filled by getline() from a file - getline(&dirPath,&dirlen,file) and that is why its ending with "\n" – Aviv Eyal Dec 06 '17 at 21:13

0 Answers0