strrchr, but...
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/"
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.
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.
uhm..what? thats not what i was talking about lol...the "/" i said refers to the "/" in filenames, like "c:/windows/"
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 );
sorry your code doesnt seem to do anything (i output directory..nothing, in fact i get invalid page fault)
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).
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).
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.
It compiles and runs perfectly here (VC++ Pro 6.0, SP5, Proc. Pack ).
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 ).
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?
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.
its ''/'' not "/" as well.
--------------------------
Those who dance are considered insane by those who cannot hear the music.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement