Thread: Remove file directory from string

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24

    Remove file directory from string

    hey all

    i have a string that contains the directory to a file, for example:

    Code:
    C:\TEST\sample.txt
    i was wondering if anyone knew a function that returned the file name only like this:

    Code:
    sample.txt
    ive been looking for awhile now and can't find anything like that.

    anyone know what it is?
    thanks in advanced.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use strrchr() to find the last \
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Not everything is wrapped in a function. Sometimes you actually have to write it yourself. Look up strrchr and strcpy for a reasonably effective method of splitting the path from the file name.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    This simple program was created in TurboC++ explorer and is used to do the job.
    But i have a question on my own. Why when i use the delimiter '\' to search for that character it doesn work? It says that the character is not found? I used the value 0x5c too as is in ASCII table and nothing, though it works for every other character, i surely miss something, give me your thoughts on that.

    Code:
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    //Function SplitString splits a string according to user input.
    char *SplitString(const char *mainString, const char delimiter)
    {
    	if(mainString == NULL)
    	{
    		printf("Can not split an empty string.\n");
    		return NULL;
    	}
    	else
    	{
    		//Index.
    		int ptrIndex = 0;
    		//Size of the return string.
    		int ptrSize = 0;
    		//The return string.
    		char *ptrString = NULL;
    		//Strrchr pointer.
    		char *ptrStr = NULL;
    		//Find the last occurance of delimiter in main string.
    		if((ptrStr = strrchr(mainString, delimiter)) == NULL)
    		{
    			printf("Error, Can not find the '%c' delimiter in main string.\n", delimiter);
    			return NULL;
    		}
    		else
    		{
    			ptrIndex = mainString - ptrStr;
    			ptrIndex = ptrIndex > 0 ? ptrIndex : -ptrIndex;
    			//Go to the next character.
    			ptrIndex++;
    			//Save size.
    			ptrSize = (int)strlen(mainString) - ptrIndex;
    			//Get space.
    			ptrString = calloc(ptrSize + 1, sizeof(char));
    			strcpy(ptrString, &mainString[ptrIndex]);
    			//Return the string.
    			return ptrString;
    		}
    	}
    }
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    	char *mString = "C:\Documents and Settings\myfile.txt";
    	char del = 's';
    	char *r = SplitString(mString, del);
    	printf("Final String: %s\n", r);
    	printf("Hit any key to exit........");
    	getch();
    	return 0;
    }
    //---------------------------------------------------------------------------

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include <conio.h>
    > #pragma hdrstop
    Can you keep the non-standard things out of your examples please.

    > #pragma argsused
    If you've no intention of using argc,argv, just leave them out and go with int main ( )

    > Why when i use the delimiter '\' to search for that character it doesn work?
    Because you should have written
    char *mString = "C:\\Documents and Settings\\myfile.txt";
    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.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by Salem View Post
    > #include <conio.h>
    > #pragma hdrstop
    Can you keep the non-standard things out of your examples please.

    > #pragma argsused
    If you've no intention of using argc,argv, just leave them out and go with int main ( )

    > Why when i use the delimiter '\' to search for that character it doesn work?
    Because you should have written
    char *mString = "C:\\Documents and Settings\\myfile.txt";
    I will stop drinking beers and using the computer after....Yes you have right once again...
    For #pragma you are right i wanted to use argc initially but later i decided that this would have no use.
    Nevertheless thanks Salem....

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Also, even though your function is pretty decently coded(Except from the things Salem mentioned), you should refrain from making the function itself print error messages, if you intend on sharing it - rather make it return values depending on the errors.

    Yes, I know, you return a pointer to a char, but you could also make it return an int and add an extra argument where the final string is stored(This is microsoft's way of doing it, for example)

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by IceDane View Post
    Also, even though your function is pretty decently coded(Except from the things Salem mentioned), you should refrain from making the function itself print error messages, if you intend on sharing it - rather make it return values depending on the errors.

    Yes, I know, you return a pointer to a char, but you could also make it return an int and add an extra argument where the final string is stored(This is microsoft's way of doing it, for example)
    You mean something like this: char *foo(char *s, int *error) and inside the function for example *error = 1 or in onother error case *error = 2.........
    Think its the same....

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use the function signature that you have there but move the error printing to the calling function. But I think that IceDane meant something like this:
    Code:
    int foo(const char *from, char **to);
    On the other hand, why do you need to create a new string anyway? Just strrchr() by itself would work reasonably well . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24
    thanks for the replies
    i got it working using strrchr()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM