sprintf being weird and giving me a segfult.

This is a discussion on sprintf being weird and giving me a segfult. within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a program to list files in a dir specified by a user, find all video ...

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    sprintf being weird and giving me a segfult.

    Hi, I'm trying to make a program to list files in a dir specified by a user, find all video files with magic and convert them so that the psp will play them. However I'm trying to pass the directory and file name to a function and in the function add them together with sprintf, but it's not working. No idea what's wrong with it, I'd be extremely grateful for any help. Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <magic.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int mencoder_move()     {
            return 0;
    }
    
    int assesment(char *file_from_dir, const char *dir_to_conv)     {
            char *full_path;
    
            sprintf(full_path,"combined = %s%s\n", dir_to_conv, file_from_dir);
    /*      strcat(full_path, file_from_dir);
            printf("%s\n %s\n %s\n", full_path, file_from_dir, dir_to_conv);
    */      
            return 0;
    }
    
    int magic_database(void)        {
            magic_t magic_cookie;
            magic_cookie = magic_open(MAGIC_MIME);
                    if (magic_cookie == NULL) {
                    printf("unable to initialize magic library\n");
                    return 1;
                        }
            printf("Loading default magic database\n");
                if (magic_load(magic_cookie, NULL) != 0) {
                    printf("cannot load magic database - %s\n", magic_error(magic_cookie));
                    magic_close(magic_cookie);
                    return 1;
                }
            return 0;
    }
    
    int main(int argc, char *argv[])
    {
            /*find directory needed to convert*/
            if (argc != 3)  {
                    printf("> Howto: %s <directory to convert> <directory to move converted files to>\n", argv[0]);
                    return 1;
            }
            /*make directory to transfer files to*/
            if (mkdir(argv[2], S_IRWXU) != 0)       {
                    printf("Could not create directory.\n");
                    struct stat st;
                    if (stat(argv[2], &st) == 0)
                            printf("%s already exists.\n", argv[2]);
                    else
                            return 1;
            }
            
            magic_database();
            
            const char *user_dir;
            user_dir = argv[1];
            printf("Converting video files in: %s\n", user_dir);
                    
            DIR *dir_convert;
            struct dirent *listing;
            dir_convert = opendir(user_dir);
            /*listing dir*/
            if (dir_convert != NULL)        {
                    while ((listing = readdir(dir_convert)))        {
                            assesment(listing->d_name, user_dir);
                    }
                    (void) closedir (dir_convert);
            }
                    else
                            perror("Could not open the directory"); 
            return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redruby147 View Post
    it's not working. No idea what's wrong with it
    Having you tried printing full_path out inside assesment() after the sprintf, to see what it is? At a glance I'd say you might want %s\%s (or / on *nix)...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    char *full_path;
    sprintf(full_path,"combined = %s%s\n", dir_to_conv, file_from_dir);
    Well duh!,
    What did you expect when you didn't allocate any space for your pointer?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Well it has a segmentation fault with sprinf, I've also tried printing out file_from_dir and dir_to_conv and they hold what they're supposed to.

  5. #5
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Are you sure that would be the problem Salem? It's just that there's quite a bit of free memory.

    Code:
                       total       used       free     shared    buffers     cached
    Mem:          3958       1362       2596          0        114        684
    -/+ buffers/cache:        562       3395
    Swap:         3153          0       3153
    Excuse my ignorance if I'm wrong.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Thre might be memory available, but you did not request for any of it, yet decided to use it anyway. Trespassers will be prosecuted!
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Thre might be memory available, but you did not request for any of it, yet decided to use it anyway. Trespassers will be prosecuted!
    Meaning full_path must be officially allocated it's own space. It may work without that but probably not for long and not reliably -- it's overwriting unallocated (free) memory instead.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > char *full_path;
    Try with something like
    char full_path[500];
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    SourceForge.net: Common mistakes and errors - cpwiki
    Besides that, have anyone told you that your indentation sucks?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    I trespassed and it looks like I got shot! Allocated some memory for it and it's working now, thanks for the help. Yes the indentation is terrible, but it's fixed now.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Try to make sure it's always fixed, and not an afterthought. We're not telling you to indent because we like it. It's because it's to help YOU, the programmer. And to others reading the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird errors gcc : functions like floor(), ceil(), sqrt()
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-04-2005, 10:00 AM
  2. weird errors gcc: functions like floor(), ceil(), sqrt()
    By gemini_shooter in forum Linux Programming
    Replies: 2
    Last Post: 06-04-2005, 09:39 AM
  3. some really weird problems with string !
    By mellisa in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2003, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21