Thread: _splitpath giving me errors!

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    _splitpath giving me errors!

    Hello everyone!
    Alright. Why does my program crash after this line?

    Code:
    char * getPartName(char * fileName, int part)
    {
        char *newName;
        
        _splitpath("C:\\Documents and Settings\\Kixdemp\\Desktop\\Mythax\\screenshot.png", NULL, NULL, newName, NULL); // << This one
        
        printf("Checkpoint!");
        
        printf(newName);
        
        return newName;
    }
    I get a "mythax.exe has encountered a problem and needs to close. We are sorry for the inconvenience." error... Why is it? Thanks!

    BTW: Also, _splitpath is cross-platform, not just Windows, right? And, does the path have to be full? What if the user enters just a filename in the command line? (A file that's on the same dir.)

  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
    > Why does my program crash after this line?
    - You didn't allocate space for your pathname

    char newName[100];
    Would be an improvement (just), but then you have the problem of returning a pointer to a local variable.

    static char newName[100];
    Is a bit better, but you can't call the function twice without trashing the result of the first call.

    It's generally best if the caller supplies a parameter of where to put the result.

    > printf(newName);
    - The result contains a %, which then blows up your poor use of printf() ?
    Never pass an unknown or user string to printf() as the control string. Any % in there will blow up big time.
    Use printf("%s\n", newName );



    > _splitpath is cross-platform, not just Windows, right?
    No it isn't.
    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
    Jun 2006
    Posts
    3
    >> _splitpath is cross-platform, not just Windows, right?
    > No it isn't.

    Oh, forget it then... Is there any other way of retrieving a filename without the extension? Thanks!
    Last edited by Kixdemp; 06-08-2006 at 06:18 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Kixdemp
    >> _splitpath is cross-platform, not just Windows, right?
    > No it isn't.

    Oh, forget it then... Is there any other way of retrieving a filename without the extension? Thanks!
    Parse the data yourself. Figure out what you need to search for in the original path string to get only the filename and create a new buffer (or use one passed into the function) to store/return the necessary data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Hmm... Yeah, I've tried that, but it didn't work... Then I tried _splitpath(), but since that isn't cross-platform, I'll have to try the former again. Thanks!

    [edit] Forget that! Too much damn work. I'll just have the user optionally select a name in the command line, and if they don't, then I'll just try to add the extension to the filename. [/edit]
    Last edited by Kixdemp; 06-08-2006 at 07:03 AM.

  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
    > Is there any other way of retrieving a filename without the extension?
    1. The whole world doesn't use \ as a separator, many other popular OS'es use /
    2. you can use strrchr() to find the last instance of a char in a string, which is useful for finding where the path ends and the filename starts.
    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
    Jun 2006
    Posts
    3
    My comments are in red.

    Code:
    char * getPartName(char * fileName, int part)
    {
        char *newName;
    
    newName not allocated!
        
        _splitpath("C:\\Documents and Settings\\Kixdemp\\Desktop\\Mythax\\screenshot.png  ", NULL, NULL, newName, NULL); // << This one
        
        printf("Checkpoint!");
        
        printf(newName);
        
        return newName;
    
    ERR returning a local variable!!
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know you can return pointers, right? How the hell do you think malloc works?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    1) What does 'newName' point to ? Its a local pointer....not pointing to any valid address up the stack!!
    2) If you return it and reference it outside this function your code will crash!!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1) What does 'newName' point to ? Its a local pointer....not pointing to any valid address up the stack!!
    True, but it doesn't have to point to stack memory.
    newName = malloc( 100 );
    would fix that problem.

    > 2) If you return it and reference it outside this function your code will crash!!
    It's already broken because it's unallocated, not because it's returned. If it's pointing to valid memory (say via malloc), then returning the pointer is just fine.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM