Results 1 to 7 of 7
Hi everybody!
Can someboby help me with a problem?
I write a C program that searches for a file in a file tree starting from a given directory.
Code:
#include ...
- 03-26-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 6
Segmentation fault!
Hi everybody!
Can someboby help me with a problem?
I write a C program that searches for a file in a file tree starting from a given directory.
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
void searchFile(char *dirName, char *fileName)
{
DIR* dir;
struct dirent *dirEntry;
struct stat inode;
char name[1000];
dir = opendir(dirName);
if (dir == 0) {
perror ("Eroare deschidere fisier");
exit(1);
}
while ((dirEntry=readdir(dir)) != 0) {
sprintf(name,"%s/%s",dirName,dirEntry->d_name);
lstat (name, &inode);
// test the type of file
if (S_ISREG(inode.st_mode))
{
if(strcmp(dirEntry->d_name,fileName) == 0)
printf("The file is in the directory %s",dirName);
}
else
if (S_ISDIR(inode.st_mode))
{
searchFile(dirEntry->d_name,fileName);
}
else;
}
}
int main()
{
char *dName;
char *fName;
printf("The name of the starting directory :");
scanf("%s",&dName);
printf("The name of the searching file :");
scanf("%s",&fName);
searchFile(dName,fName);
}
I get a message with "segmentation fault". Can anyone tell me what is that and how can a solve this problem?
Thanks a lot,
Anayd
- 03-26-2007 #2
Funny explanation I once heard:
Q: What does "Segmentation Fault" mean?
A: It's Ancient UNIX for "**** You"
What it really means is that a section of your code tries to access memory that it is not supposed to, or tries to write to memory that is ro.
Sorry I see nothing obvious by looking quickly, but I am not the greatest C hacker or anything.
- 03-26-2007 #3Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
These are pointers to chars, first you have to allocate memory for them. Check the malloc() function in your manual.Code:char *dName; char *fName;
Instead of pointers you can use char arrays:
RegardsCode:char dName[1000], fName[1000];
- 03-26-2007 #4
Ah there we go, <flashback to college daze> lol
- 03-26-2007 #5Just Joined!
- Join Date
- Mar 2007
- Posts
- 6
Thanks!
Thanks a lot Franklin52 and likwid! I really appreciate your help, but now I get another error "too many open files". Doesn't recursivity work in this situation?
- 03-27-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
You must avoid to read the directories ("." and "..") and the call
must be:Code:searchFile(dirEntry->d_name,fileName);
And close the stream associated with dir.Code:searchFile(name,fileName);
This should do it:
RegardsCode:#include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <fcntl.h> #include <stdio.h> void searchFile(char *dirName, char *fileName) { DIR* dir; struct dirent *dirEntry; struct stat inode; char name[1000]; dir = opendir(dirName); if (dir == 0) { perror ("Eroare deschidere fisier"); exit(1); } while ((dirEntry=readdir(dir)) != 0) { sprintf(name,"%s/%s",dirName,dirEntry->d_name); lstat (name, &inode); // test the type of file if (S_ISREG(inode.st_mode)) { if(strcmp(dirEntry->d_name,fileName) == 0) printf("The file is in the directory %s\n",dirName); } else if (S_ISDIR(inode.st_mode)) { if (strcmp(dirEntry->d_name,".") != 0 && strcmp(dirEntry->d_name,"..") != 0) { searchFile(name,fileName); } } } closedir(dir); } int main() { char dName[1000]; char fName[1000]; printf("The name of the starting directory :"); scanf("%s",&dName); printf("The name of the searching file :"); scanf("%s",&fName); searchFile(dName,fName); }
- 03-29-2007 #7Just Joined!
- Join Date
- Mar 2007
- Posts
- 6
Thanks Franklin52!
Thanks a lot, I really forgot about the directories "." and ".."
I appreciate very much your help!


Reply With Quote