Click here to Skip to main content
Sign Up to vote bad
good
i used this code but it gives me segment fault ?
char* path;
 
scanf("%20[^\r]",path);
DIR* dir = opendir(path);
if (dir)
{
printf("Directory Exists");
closedir(dir);
}
else if (ENOENT == errno)
{
printf("Directory Exist");
}
else
{
printf("Directory not Exist");
}
Posted 6 Feb '13 - 6:25

Comments
FatimaAsif - 6 Feb '13 - 12:28
i give this input /home/fatima/Desktop/child

2 solutions

Your path variable is just a pointer to char. But you must provide the space to store the scanned string including the terminating NULL char:
char path[21];
  Permalink  
Comments
FatimaAsif - 6 Feb '13 - 12:48
ok it works thanks for your help.
Jochen Arndt - 6 Feb '13 - 12:52
Thank you for your feedback. You may 'Accept' the answer using the so named link to mark it as solved.
FatimaAsif - 6 Feb '13 - 12:55
that problem is solved bt another problem is that it only returns Directory exist, how can i solved it , can u please help me?
Jochen Arndt - 6 Feb '13 - 13:00
That's another problem. But the answer is easy. Take a close look on your code: else if (ENOENT == errno) { printf("Directory Exist"); } You are printing "Directory Exist" when it does not exist.
FatimaAsif - 6 Feb '13 - 14:15
but if exist or not it gives the same output
Jochen Arndt - 7 Feb '13 - 1:58
I hope you have seen it meanwhile. The servers where very busy, so that I could not answer. Change your code to: else if (ENOENT == errno) { printf("Directory does not exist"); } else { printf("Not a directory or other error"); }
path must be a buffer, not a char pointer. Where should scanf deposite string being read? For example
 
char path[100];
scanf ("%20[^\r]", path);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,356
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid