• Advertisement

Archived

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

strrchr, but...

This topic is 5973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

  • Advertisement