Thread: Problem with strcat()

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Problem with strcat()

    What's wrong with my functions ? I need to check if the files found are dirs! but when a use strcat() for sum de localdir and the name.... puff !! segment fault !!! thanks

    Code:
    void scan (char *localDir) {
        struct dirent **namelist;
        int n;
        n = scandir(localDir, &namelist, 0, alphasort);
        if (n < 0)
            perror("scandir");
        else {
            while(n--) {
    	    char *filename = strcat(localDir, namelist[n]->d_name);
    	    if (isDir(filename) {
                        printf("%s \n", filename);
    	    }
                free(namelist[n]);
            }
            free(namelist);
        }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say you have
    .
    ..
    foo.dat
    bar.txt

    > char *filename = strcat(localDir, namelist[n]->d_name);
    localDir is going to end up being one long long string of
    ...foo.datbar.txt

    Are you sure it's
    a) what you wanted to do
    b) long enough
    c) initialised with a suitable \0 ending string to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Salem,

    I need to check if the found files are a directories.. is simple. but I am never coder C before.
    I can make the software print the file names on the screen... but, I need use, something like strcat(), for union DIRNAME (where scandir() search files) and the FILENAMES (namelist[n]->d_name). So, I can check with:
    isDir(filename) !
    thanks for your help

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What's wrong with
    isDir(namelist[n]->d_name)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    if I am not on the current dir ? ...namelist[n]->d_name is just the file name
    Last edited by souza; 11-09-2008 at 01:57 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you really want to create
    mydir/.
    mydir/..
    mydir/foo.dat
    mydir/bar.txt

    Perhaps something along these lines, inside the loop
    char temp[size]; // you pick a size
    strcpy( temp, dir );
    strcat( temp, file );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    thats it !! thanks a lot Salem.
    I dont know why, but strcpy() save my code !

    cya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 06:15 AM
  2. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 02:31 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 08:05 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM