Thread: opendir fail

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    opendir fail

    open dir return null and i don't now why .

    the code is :
    Code:
    int main(){
    
    char buff[1000]; 
    
    strcpy(buff,"."); 
    
    lsFunc(buff); 
    
    
    }
    
    
    void lsFunc(char* toSend){
      
      void* myDir;
      struct dirent* ent; 
    
    
      
      if(!strcmp(toSend,""))  myDir=opendir(".");
      else			 myDir=opendir("toSend"); 
    
    perror(NULL); 
    printf("%d\n",errno);
    }
    and the output is :
    "No such file or directory
    2
    Segmentation fault"

    Thank you for your help
    eyal

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
      if(!strcmp(toSend,""))  myDir=opendir(".");
      else			 myDir=opendir("toSend"); 
    
    perror(NULL);
    The quotes are wrong. Let's assume if is true, then you open dot, but if it's false, you open "toSend" literally, not the value of toSend, whatever it is.

    perror (NULL); always executes and is likely to seg-fault since perror will access the pointer to print that value to stderr, and accesses to NULL are undefined behavior.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    i am idiot .
    thank you !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opendir() returns null but errno is zero
    By Elkvis in forum Linux Programming
    Replies: 2
    Last Post: 12-16-2008, 01:16 PM
  2. opendir failure
    By MK27 in forum C Programming
    Replies: 5
    Last Post: 07-10-2008, 03:47 PM
  3. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  4. php: glob() vs opendir() and friends
    By MisterSako in forum Tech Board
    Replies: 1
    Last Post: 03-16-2006, 01:42 AM
  5. opendir(), readdir()
    By BigDaddyDrew in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2003, 12:22 PM