Why are some elements missing after using strcat()
Hello,
I am trying to split the unix folder path from the file name that is entered. For example, if the path entered is "/home/user/temp/asdf.txt" I want to return 2 things... Folder name: "/home/user/temp" File Name: "asdf.txt" Code:
#include <stdio.h> Code:
text[/The/quick/brown/fox/jumped/over/the/lazy/red/dog] Kindly help me if i am wrong anywhere... Thank you. -Monil |
Not a solution to your question, but why not to search the filename from the end of file? Short for loop searching the characters will be faster and probably shorter.
|
Right, But, I also need the directory path too...
then, how do i remove the path and the file name? Any idea? -Monil |
it's happening because strtok() modifies the string; it doesn't allocate new ones. So wlist is an array of pointers into the string text, which has its /'s turned into '\0's. When you strcat() onto each of those pointers, it writes a '\0' into the first character of the string pointed to by the next pointer. (Actually, this is only true for every other pointer, since for half of them strcat sees a 0 length first argument and overwrites a character that is not in any existing string anymore.)
That's not very clear as it's way too early in the morning. Basically you just need to realize what strtok is doing to your original string. |
also, see dirname(3)
|
All times are GMT -5. The time now is 11:23 PM. |