Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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.

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    char *dName;
    char *fName;
    These are pointers to chars, first you have to allocate memory for them. Check the malloc() function in your manual.
    Instead of pointers you can use char arrays:
    Code:
    char dName[1000], fName[1000];
    Regards

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Ah there we go, <flashback to college daze> lol

  6. #5
    Just 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?

  7. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631

    Lightbulb

    You must avoid to read the directories ("." and "..") and the call

    Code:
    searchFile(dirEntry->d_name,fileName);
    must be:

    Code:
    searchFile(name,fileName);
    And close the stream associated with dir.

    This should do it:

    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\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);
    
    }
    Regards

  8. #7
    Just 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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •