Thread: Pointer Data Getting Cut Off

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Pointer Data Getting Cut Off

    I've never seemed to be very good at pointers and decided to practice by writing a function to get the file extension of a given filename. It seems that it is working correctly for most filepaths except in the case of extremely long ones (not that this happens much but I still would like my function to support it).

    It cuts off at the 'k' in the third alphabet.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void getext(const char *path, char *ext);
    
    int main (void)
    {
    	char *path = "myfile.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    	char *ext = malloc(FILENAME_MAX * sizeof(char));
    	
        getext(path, ext);
        printf("%s\n", ext);
    
        return 0;
    }
    
    void getext(const char *path, char *ext)
    {
    	char *pch;
    	
    	pch = strrchr(path, '.');
    	
    	memcpy(ext, pch + 1, *path - *pch);
    }
    By the way, FILENAME_MAX = 2048
    Any pointers/critiques welcome! Happy New Year!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you expect *path - *pch to be?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you want to subtract the pointers in your memcpy line, rather than subtracting the CONTENTS at the pointer. [Also, make sure you copy the right amount - I haven't checked your calculations, but you want to copy 1 byte beyond the actual extension length, so that you get the terminating zero as well].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Why not simply use strlen() instead.
    I thought file extensions would be normally three or four characters long(in Windows of course)
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by stevesmithx View Post
    Why not simply use strlen() instead.
    I thought file extensions would be normally three or four characters long(in Windows of course)
    In traditional FAT, the extension is 3 characters. But modern file-systems (including Unix ones that have been around for 20+ years) that allow long filenames are able to have as long an extension as the max filename size allows. This obviously includes the extended (long) FAT filenames.

    To the original poster, what happens in your code with this:
    Code:
    char *fname = "foo/bar.baz/blah";
    char ext[MAX_PATH]; 
    getext(fname, ext);
    [be aware also that slashes in Windows can be either forward or backward ones (at the C library level - the OS itself expects backslashes, '\\', whilst Unix/Linux only allows forward slashes for directory separators].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thanks for clarifying, Mats.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    The only problem I am having now is that when no extension is found, I want to return a null pointer, but for some stupid reason I am not seeing, I don't know how to make it a null pointer can anyone help me out. The line I am trying to set it to null is highlighted in red.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void getext(const char *path, char *ext);
    
    int main (void)
    {
    	char *path = "foo/bar.baz/blah";
    	char *ext = malloc(FILENAME_MAX * sizeof(char));
    	
        getext(path, ext);
        if (ext == NULL)
        	printf("No extension found\n");
        else
        	printf("Extension: %s\n", ext);
    
        return 0;
    }
    
    void getext(const char *path, char *ext)
    {
    	char *pch;
    	
    	pch = strrchr(path, '/');
    	if (pch != NULL)
    		pch = strrchr(pch, '.');
    	else
    		pch = strrchr(path, '.');
    
    	if (pch == NULL)
    		*ext = NULL;
    	else
    		memcpy(ext, pch + 1, strlen(pch));
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    *ext is not a pointer. ext, on the other hand, is.

  9. #9
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    You're right, but it still triggers the else block in my main() if statement.

    It just looks like
    Extension:
    Like its null, lol

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    and you can drop the 'sizeof(char)' which is 1 by definition.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    for the null pointer problem, either work on [...temporarily removed, cf next post...] or more simply make your function returns 1 if it succeeds or 0 if it fails.

    PS: *ext=NULL sets 0 as the first item of the string, ext=0 set the pointer to 0 but this is only a local variable so it has no effect once you're out of the function
    Last edited by root4; 01-01-2009 at 01:48 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by carrotcake1029 View Post
    You're right, but it still triggers the else block in my main() if statement.

    It just looks like

    Like its null, lol
    Of course it is. The contents of the pointer is copied into the new local pointer in the function, and its contents gets destroyed when the function ends.
    That is why we use pointers in the first place - to modify data in a local variable in another function.
    But if you want to modify a pointer to set its value to NULL, how do we do now? Oh dear.
    Think about it a while. Since a pointer is merely a variable, it must be able to be done the same way as regular variables, no?
    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.

  13. #13
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    I think the problem is caused by faulty logic.
    you expect pch to be null but its not.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  14. #14
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well, I am kind of depending on strrchr's return value
    Return Value
    A pointer to the last occurrence of character in str.
    If the value is not found, the function returns a null pointer.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by stevesmithx View Post
    I think the problem is caused by faulty logic.
    you expect pch to be null but its not.
    Nope, str(r)chr() returns NULL if the searched for item is not found.

    However, as Elysia says, you can not modify what a pointer points to, unless you pass a pointer to pointer. [Or you could return a pointer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM