Re: sub string



rindex does the same thing as strrchr.  Couldn't tell you if either one
is POSIX off the top of my head.  They would have the advantage over
your approach because
a) You don't have to know how many directories deep you are.
b) As you pointed out, strtok is destructive to the original string,
which you may not want.

Yes, strrchr(aux, "\"); is the best approach in this case.
This is indeed a AINSI C standard function (string.h).

Just for the fun of it, it would take a bit more
to get the full solution using strtok:

srtcpy (save_string, string);
ptr = strtok (string,"\");
do {
   file = ptr;
   ptr = strtok (NULL,"\");
   } while (ptr != NULL);

Regarding rindex, I found it in /usr/include/string.h,
in the section below, so apparently this call is
recognized only in BSD-like systems.

Carlos

---------------/usr/include/string.h------------------

#if defined(__USE_BSD) || defined(__USE_XOPEN_EXTENDED)
/* Copy N bytes of SRC to DEST (like memmove, but args reversed).  */
extern void bcopy __P ((__const __ptr_t __src, __ptr_t __dest, size_t __n));

/* Set N bytes of S to 0.  */
extern void bzero __P ((__ptr_t __s, size_t __n));

/* Compare N bytes of S1 and S2 (same as memcmp).  */
extern int bcmp __P ((__const __ptr_t __s1, __const __ptr_t __s2, size_t __n));

/* Find the first occurrence of C in S (same as strchr).  */
extern char *index __P ((__const char *__s, int __c));

/* Find the last occurrence of C in S (same as strrchr).  */
extern char *rindex __P ((__const char *__s, int __c));

/* Return the position of the first bit set in I, or 0 if none are set.
   The least-significant bit is position 1, the most-significant 32.  */
extern int ffs __P ((int __i));

/* Compare S1 and S2, ignoring case.  */
extern int __strcasecmp __P ((__const char *__s1, __const char *__s2));
extern int strcasecmp __P ((__const char *__s1, __const char *__s2));

/* Compare no more than N chars of S1 and S2, ignoring case.  */
extern int __strncasecmp __P ((__const char *__s1, __const char *__s2,
                               size_t __n));
extern int strncasecmp __P ((__const char *__s1, __const char *__s2,
                             size_t __n));
#endif /* Use BSD or X/Open Unix.  */

#ifdef  __USE_BSD
-------------------------/usr/include/string.h------------




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]