extract filename from filepath, problem handling strings

This is a discussion on extract filename from filepath, problem handling strings within the C Programming forums, part of the General Programming Boards category; Hello I am trying to use a method that extracts the filename out of a filepath but I cant seem ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41

    extract filename from filepath, problem handling strings

    Hello
    I am trying to use a method that extracts the filename out of a filepath but I cant seem to make it work. The code worked ok within another function, but now when I try to separate it into its own function it doesnt work anymore. =/
    This is my function
    Code:
    #include "gbs.h"
    
    char * extract_filename(char *str){
    	
    	int ch = '\\', result, len;
    	char  *pdest, line[250], *inpfile = {NULL};
    
    	// Search backwards for last backslash in filepath /
    	inpfile = (char *)malloc(200*sizeof(char));
    	pdest = strrchr(str, ch);	result = pdest - str + 1;
    	
    	// if backslash not found in filepath
    	if(pdest == NULL ){
    		printf( "Result:\t%c not found\n" );
    	}
    	
    	// extract filename from file path /
    	len = strlen(str) - result - 1;
    	strncpy(inpfile, &line[result], len);
    	inpfile[len] = NULL;
    	return inpfile;
    }
    The function is declared in the header gbs.h. I try to call the function from another function like this. I omitted most of the code to not make the post shorter.
    Code:
    void anotherfunction()
    {
            char *filename, filepath[200];
    
            //some code
    
    	fscanf(fp,"%s",filepath);
    	printf("%s\n",filepath);
            
    	filename= extract_filename(filepath);
            printf("%s\n",filename);
    }
    The printf("%s\n",filepath); is printed OK, but the output from printf("%s\n",filename); is this: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠kkkk
    I am new to this and I would be very thankful for any ideas!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the content of "line"? You probably want to remove that variable, and use "str" instead.

    Also, you are mallocing unnecessarily large space - seeing as you calculate len of the string [although it's one off].

    And you could simplify everything by using pdest in itself - something like this:
    Code:
    char * extract_filename(char *str)
    {
        int     ch = '\\';
        size_t  len;
        char   *pdest;
        char   *inpfile = NULL;
    
        // Search backwards for last backslash in filepath 
        pdest = strrchr(str, ch);
    	
        // if backslash not found in filepath
        if(pdest == NULL )
        {
    	printf( "Result:\t%c not found\n", ch );
    	pdest = str;  // The whole name is a file in current path? 
        }
        else 
        {
    	pdest++; // Skip the backslash itself.
        }
    	
        // extract filename from file path
        len = strlen(pdest);
        inpfile = malloc(len+1);  // Make space for the zero.
        strncpy(inpfile, pdest, len+1);  // Copy including zero. 
        return inpfile;
    }
    Please, before you copy and paste my answer, understand what went wrong in your code and try to fix it. That will teach you a lot more than copy'n'paste-skills can.

    --
    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.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Brilliant help =D
    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with strings and filenames
    By papagaio in forum C Programming
    Replies: 3
    Last Post: 12-27-2008, 06:00 AM
  2. Replies: 10
    Last Post: 06-10-2008, 02:17 AM
  3. Problem with strings
    By PPhilly in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 06:56 PM
  4. Problem with seperating strings with delimiters..
    By hykyit in forum C Programming
    Replies: 2
    Last Post: 03-29-2006, 05:51 PM
  5. Separating filename from filepath
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2003, 11:37 AM

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