strrchr()

John Mundt john at chinet.chi.il.us
Wed Dec 27 04:33:16 AEST 1989


In article <1128 at polari.UUCP> jeffery at polari.UUCP (Jeffery Foy) writes:
>
>Can someone tell/show me how to use this function?  I've never used it
>before and some code that I got recently has it.  Any help will be
>appreciated muchly...
> 

Assume you have a path to a file and want to find just the file name.
You could have just a relative path, foo.dat, or an absolute path,
/usr/you/docs/foo.dat.  Assume that the path is in char *path.

char *ptr, path[BUFSIZ];
	...

	ptr = strrchr(path, '/');
	printf("File is %s\n", ptr ? ptr + 1 : path);

	...

ptr will hold the location of the last / in the path, so ptr + 1
points to the file name.  If strrchr comes back as NULL, then the
pathname is relative.  strchr() does the same thing but finds
the first instance.  BSD uses index() and rindex() to do the same
thing.  

-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john at admctr.chi.il.us *OR* fred at teacha.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  



More information about the Comp.lang.c mailing list