Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 14-05-2011, 04:10 PM   #1 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Error in C program


Hello friends, I am creating C program that will list all the files in the directory specified with their paths. So code is here.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>

void declevel(char str[])
{
    int count=0;
    int i=0,j=0;
    
    for (i=0;i<strlen(str);i++)
    {
        if (str[i] == '/')
        {
            count++;
            }
        }
    
    int arr[count];
    
    for (i=0;i<strlen(str);i++)
    {
        if (str[i] == '/')
        {
            arr[j] = i;
            j++;
            }
        }
    str[arr[count-1]] = '\0'; 
    printf("%s",str);      
    }    

void getdirstruct(char dir[])
{
    DIR *dp;
    struct dirent *entry;
    struct  stat statbuf;
    char path[1000], path2[1000];
    int s;
    if ((dp = opendir(dir)) == NULL)
    {
        return;
        }
    
    chdir(dir);
    strcat(path,dir);
    
    while ((entry = readdir(dp)) != NULL)
    {
        lstat(entry->d_name,&statbuf);
        if (S_ISDIR(statbuf.st_mode))
        {
            if (strcmp(".",entry->d_name) == 0 || strcmp(".", entry->d_name) == 0)
            {
                s = sprintf(path2,"%s",path);
                declevel(path2); /*directory level decreases*/
                continue;
                }
            getdirstruct(entry->d_name);
            strcat(path,entry->d_name);
            }
        }
    chdir(".");
    closedir(dp);
        }

void main()
{
    getdirstruct("/home");
    }
Here declevel is a function (or more specifically procedure) that will decrease the path string with level 1. I mean if path string is /media/sda1/downloads then after calling declevel this path string will be /media/sda1. It runs very well when I run it independently in another program. But here I am getting error. Compiler (gcc) compiles it without error, but when I run it in terminal I got error segmentation fault (core dumped). Please help me friends.!!!
abhijangda is online now   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 14-05-2011, 08:42 PM   #2 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Error in C program

i don't have much knowledge about file handling but i tested it and found that the segmentation fault was being caused because
Code:
str[arr[count-1]] = '\0';
in decleval(). when .config is passed to decleval(), count is zero and str[arr[-1]] causes segmentation fault.
i think there is some problem in
Code:
if (strcmp(".",entry->d_name) == 0 || strcmp(".", entry->d_name) == 0)
you might have missed something here. also you have same statements in both sides of "||"!!
can you please explain to me why you added this "if" condition(don't know much about advanced file handling, so just curious!)?
nims11 is online now   Reply With Quote
Old 14-05-2011, 10:56 PM   #3 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: Error in C program

Quote:
Originally Posted by nims11 View Post
i don't have much knowledge about file handling but i tested it and found that the segmentation fault was being caused because
Code:
str[arr[count-1]] = '\0';
in decleval(). when .config is passed to decleval(), count is zero and str[arr[-1]] causes segmentation fault.
i think there is some problem in
Code:
if (strcmp(".",entry->d_name) == 0 || strcmp(".", entry->d_name) == 0)
you might have missed something here. also you have same statements in both sides of "||"!!
can you please explain to me why you added this "if" condition(don't know much about advanced file handling, so just curious!)?
oh, sorry for if statement, I corrected it just after creating thread here. the correct one is
Code:
if (strcmp(".",entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
But I cannot understand what you want to say about declevel(). What is this .config. Also count can never be zero, as I am working on Fedora 14, so path(which passed to declevel) will always contain atleas one '/'.
abhijangda is online now   Reply With Quote
Old 15-05-2011, 01:45 PM   #4 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Error in C program

there seems to be some logical problem in the program.
when i replaced
Code:
s = sprintf(path2,"%s",path);
declevel(path2); /*directory level decreases*/
continue;
with
Code:
printf("path2-%s\n",path2);
declevel(path2); /*directory level decreases*/
printf("path2-%s\n",path2);
continue;
the output was
Quote:
dname-.
path2-/home
path2-
dname-..
path2-/home
path2-
dname-nims11
dname-.adobe
dname-.
path2-.adobe
Segmentation fault
nims11 is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:28 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2