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
strrchr, but...
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.
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:
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement