Archived

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

amemorex

strrchr, but...

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


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


Link to post
Share on other sites
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 <iostream.h>
#include <string.h>

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

Share this post


Link to post
Share on other sites
strcpy() can only copy strings onto properly allocated memory. You are confusing it with strdup(). And if you use strdup() remember to free() your memory at some point.

Share this post


Link to post
Share on other sites