Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i used this code but it gives me segment fault ?
C#
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
Comments
FatimaAsif 6-Feb-13 12:28pm    
i give this input
/home/fatima/Desktop/child

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:
C++
char path[21];
 
Share this answer
 
Comments
FatimaAsif 6-Feb-13 12:48pm    
ok it works thanks for your help.
Jochen Arndt 6-Feb-13 12:52pm    
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:55pm    
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:00pm    
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:15pm    
but if exist or not it gives the same output
path must be a buffer, not a char pointer. Where should scanf deposite string being read? For example

C++
char path[100];
scanf ("%20[^\r]", path);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900