#### Archived

This topic is now archived and is closed to further replies.

# strrchr, but...

## Recommended Posts

amemorex    122
i am using strrchr to return characters after some character i point out, like *chrlast = strrchr("D:/system/image.bmp, ''/''); thats successfully returns "image.bmp" to the chrlast, but now how would i return whats BEFORE the last "/" in the string, meaning how would i extract the "D:/system/"

##### Share on other sites
Fruny    1658
To truncate a string, you just write ''\0'' at then end.
Thus, if the string you passed to strrchr is writable, you just do *chrlast = ''\0'' and, behold, the original chain has been truncated to what you wanted.

##### Share on other sites
amemorex    122
uhm..what? thats not what i was talking about lol...the "/" i said refers to the "/" in filenames, like "c:/windows/"

##### Share on other sites
Fruny    1658
I stand by my answer. Here''s some code.

  char fullpath[256];char directory[256];char filename[256];strcpy( buffer, "c:/windows/image.bmp" );char* chrlast = strrchr( fullpath, ''/'' );strcpy( filename, chrlast );*chrlast = ''\0'';strcpy( directory, fullpath );

##### Share on other sites
amemorex    122
sorry your code doesnt seem to do anything (i output directory..nothing, in fact i get invalid page fault)

##### Share on other sites
Stoffel    250
There were some simple mistakes with misnamed variables, but the code is correct. You should be able to figure it out: replace "buffer" with "fullpath".

One note: I would prefer to use strncpy instead of "destroying" fullpath by putting a null in the middle of it and using strcpy. You already know how long the string is (chrlast - buffer).

##### Share on other sites
Fruny    1658
Ok I have corrected the code a bit, since strrchr returns the pointer to the ''/'' character, and what you want is the next one.

So long as strrchr does not return NULL and that your string fits in the buffer, the example should work.

  #include "stdio.h"#include "string.h"int main(int argc, char* argv[]){ char fullpath[256]; char directory[256]; char filename[256]; strcpy( fullpath, "c:/windows/image.bmp" ); char* chrlast = strrchr( fullpath, ''/'' ) + 1; strcpy( filename, chrlast ); *chrlast = ''\0''; strcpy( directory, fullpath ); printf( "%s\n%s\n", directory, filename ); return 0;}

It compiles and runs perfectly here (VC++ Pro 6.0, SP5, Proc. Pack ).

##### Share on other sites
amemorex    122
oh...so tell me if i understand this..strrchr works like this?

  strcpy( filepath, "c:/windows/test.exe");buffer = strrchr( filepath, "/");//making buffer equal the returned string//and it also changes filepath to equal everything else in//the string?

##### Share on other sites
terminate    259
Did you ever think of using strchr instead of strrchr? strchr searches from the beggining, srrchr starts at the end.

its ''/'' not "/" as well.

--------------------------

Those who dance are considered insane by those who cannot hear the music.

##### Share on other sites
Fruny    1658
No, the second argument is a character, not a string.

##### Share on other sites
amemorex    122
okay okay, "" was a common mistake when i was typing in the post, nonetheless, was my understanding of how the function works correct?

also, i have a question (i thought i fairly understood pointers, but this totally confuses me now)

why is that

*chrlast = '\0';

truncutates the string "fullpath"? i dont understand why or how that works

Edited by - amemorex on December 14, 2001 8:13:02 PM

##### Share on other sites
Fruny    1658
The value strrchr returns is a pointer. Which points to the last character in the original string which is equal to the second argument. It does not create a new string, nor changes the original string. However, if you modify the character pointed at by the return value, you are modifying the original string. strcpy detects a ''\0'' as the end of the string to copy.

##### Share on other sites
amemorex    122
ok, i understand what you mean now, but now i'm trying to implement it and im having problems..

basically i want to kill 2 birds with 1 function, like this:

  #include #include void returninfo(char *file, char **fname){ char *tmp = strrchr(file, '/')+1); strcpy(*fname, tmp); *tmp = '\0'; (*fname)++;}void main() { char fullpath[256]; char *filename; strcpy(fullpath, "c:/windows/image.bmp"); getfile(fullpath, &filename); cout << fullpath << endl; //should be c:/windows/ cout << filename << endl; //should be image.bmp}

i realize i am doing something wrong here and getting things mixed up, can you guys tell me what to do to this to achieve my goal? i basically want the returninfo function to set fullpath to the path, and filename to the filename

Edited by - amemorex on December 14, 2001 9:07:57 PM

amemorex    122
hello?..

##### Share on other sites
Guest Anonymous Poster
hi amemorex, how are you?