Like Tree1Likes
  • 1 Post By Salem

Why wont this work?

This is a discussion on Why wont this work? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { _explode(argv[0]); system("pause"); //Yes... I know this is bad. return ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Exclamation Why wont this work?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        _explode(argv[0]);
        system("pause"); //Yes... I know this is bad.
        return 0;
    }
    
    
    void _explode(char arg[])
    {
        char delims[] = "\/";
        char *result = NULL;
        //char arg[] = "This \ is / a \message for you"; //this works
        result = strtok(arg, delims);
        while (result != NULL)
        {
            printf("Result is \"%s\"\n", result);
            result = strtok(NULL, delims);
        }
    }
    I want my programs path (argv[0]) to be passed to my _explode function. I wish that it's last entry will be the programs name. E.g. Myprogram.exe. Why wont it work?

    -Thanks.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    Well one thing I noticed is that char *result is not pointing to any valid memory. You need to allocate some memory before you start trying to assign values to it.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Thanks for the quick reply, but it points to the result of strtok(). I haven't programmed in C for quite a long time, so I have picked up some bad habits. I think there's something wrong with how I'm parsing the argument of argv[0], because it works fine if I don't parse and argument, and uncomment the local variable 'arg'.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    //char arg[] = "This \ is / a \message for you"; //this works
    Are you are saying that this piece of code gives the error? If so it is because C does not allow you to assign strings. Although I don't know if I am helping or not right now lol

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by camel-man View Post
    //char arg[] = "This \ is / a \message for you"; //this works
    Are you are saying that this piece of code gives the error? If so it is because C does not allow you to assign strings. Although I don't know if I am helping or not right now lol
    No, the above code doesn't work as I would like it to, but this code works:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        _explode();
        system("pause"); //Yes... I know this is bad.
        return 0;
    }
    
    
    void _explode()
    {
    	char delims[] = "\/";
        char *result = NULL;
        char arg[] = "This \ is / a \message for you"; //this works
        result = strtok(arg, delims);
        while (result != NULL)
        {
            printf("Result is \"%s\"\n", result);
            result = strtok(NULL, delims);
        }
    }
    With the exception of only one slash being effective at splitting the string. This is driving me crazy.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    834
    Quote Originally Posted by inu11byte View Post
    Code:
        char delims[] = "\/";
    You mean "\\/" there, don't you? Print it (printf("delims is '%s'\n", delims);), and you'll see what I mean.

    Your _explode() function will also modify argv[0] in place, leaving either an empty string or the initial path component there.

    If you just want the final component, perhaps
    Code:
    #include <string.h>
    
    #if defined(macintosh)
    #define  DIRECTORY_SEPARATOR_CHAR   ':'
    #define  DIRECTORY_SEPARATOR_STRING ":"
    #elif defined(_WIN32) || defined(_WIN32_WCE) || defined(__DOS__)
    #define  DIRECTORY_SEPARATOR_CHAR   '\\'
    #define  DIRECTORY_SEPARATOR_STRING "\\"
    #else
    #define  DIRECTORY_SEPARATOR_CHAR   '/'
    #define  DIRECTORY_SEPARATOR_STRING "/"
    #endif
    
    char *base_name(char *path)
    {
        char *final;
    
        if (!path)
            return NULL;
    
        final = strrchr(path, DIRECTORY_SEPARATOR_CHAR);
        if (final)
            return final + 1;
    
        return path;
    }
    would be better? You can then use base_name(argv[0]) whenever you need just the file name (base name) of the current process.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Ah, forgot that the backslash was an escape character... Momentary lapse of knowledge... Hahaha. You're code looks good, but base_name() wont take argv[] as a parameter. :/

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    834
    Quote Originally Posted by inu11byte View Post
    You're code looks good, but base_name() wont take argv[] as a parameter. :/
    Why should it?

    argv[] is an array of strings, argv[0] being the path used to execute the current process, argv[1] (if argc > 1) being the first command line parameter, and so on.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Never-mind, all is working well. Thank you very much! Could you tell me why my code wasn't working? Just out of curiosity. Also, why is the function base_name a pointer? Just curious.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by Nominal Animal View Post
    Why should it?

    argv[] is an array of strings, argv[0] being the path used to execute the current process, argv[1] (if argc > 1) being the first command line parameter, and so on.
    Sorry, I don't get what you mean. I wanted my path 'C:\users\*****\Desktop\myfile.exe' to just be 'myfile.exe'.

    Ah, just re-read your post. I was meaning argv[0], not argv[]. Sorry about the confusion. Your code worked as expected. Thanks.
    Last edited by inu11byte; 11-09-2012 at 10:53 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If you just want the last component, then perhaps
    p = strrchr(path,'\\');
    inu11byte likes this.
    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.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Thank you, Salem. Can anybody answer my questions? Why could I pass an the argument 'argv[0]' into my original program? And why is the point of making a function a pointer?

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    Quote Originally Posted by inu11byte View Post
    Why could I pass an the argument 'argv[0]' into my original program?
    argv[0] has type `char *' and your function accepts an argument of type `char []'. In C, these two are equivalent.

    However, the following may work but is probably not correct:

    Code:
    #include <stdio.h>
    
    void pause(){printf("Press enter to continue...\n");getchar();}
    void tokenize_arg(char a[]);
    
    int main(int argc, char *argv[])
    {
        tokenize_arg(argv[0]);
        pause();
    }
    
    void tokenize_arg(char a[])
    {
        char delims[] = "/\\";
        char *token = strtok(a, delims);
        while (token != NULL)  {
            printf("Got a token \"%s\"\n", token);
            token = strtok(NULL, delims);
        }
    }
    If I run this on my Windows machine, I get the correct output (drive name, directory, directory, then my program name). However, argv[] is not in general a modifiable array. So you should better make a copy before using strtok():

    Code:
    void tokenize_arg(char a_readonly[])
    {
        char a[strlen(a_readonly)+1];
        strcpy(a, a_readonly);
    
        char delims[] = "/\\";
        char *token = strtok(a, delims);
        while (token != NULL)  {
            printf("Got a token \"%s\"\n", token);
            token = strtok(NULL, delims);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. y wont this work this way
    By gamer4life687 in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2005, 09:39 PM
  2. Why wont this work?
    By Corallis in forum C Programming
    Replies: 2
    Last Post: 11-26-2003, 10:24 PM
  3. Why wont this work??
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2002, 07:11 PM
  4. why wont this work???
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2002, 02:25 PM
  5. gets wont work
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-31-2002, 01:31 PM

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