Originally Posted by
monil
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"
I'd choose strrchr to look for the last '/' and overwrite it with a null.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char text[] = "/home/user/temp/asdf.txt";
char *file, *path = text, *slash = strrchr(text, '/');
printf("text = \"%s\"\n", text);
if ( slash )
{
*slash = '\0';
file = ++slash;
}
printf("path = \"%s\"\n", path);
printf("file = \"%s\"\n", file);
printf("text = \"%s\"\n", text);
return 0;
}
/* my output
text = "/home/user/temp/asdf.txt"
path = "/home/user/temp"
file = "asdf.txt"
text = "/home/user/temp"
*/
[edit]But to try to answer your title question, would looking at this help?
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define WORD_COUNT 10
void show(const char *text, size_t size)
{
while(size--)
{
if(isprint(*text))
{
putchar(*text);
}
else
{
printf("%d", *text);
}
++text;
}
}
int main(void)
{
char text[] = "/The/quick/brown/fox/jumped/over/the/lazy/red/dog";
char *wlist[WORD_COUNT]; /*array of 10 pointers to characters*/
char *nwlist[WORD_COUNT];
int i;
show(text, sizeof text);
puts("\n---");
wlist[0] = strtok( text, "/" );
show(text, sizeof text);
printf(", wlist[%d] = \"%s\"\n", 0, wlist[0]);
//nwlist[0] = strtok( text, "/" );
//show(text, sizeof text);
for ( i=1; i < WORD_COUNT; i++ )
{
wlist[i] = strtok( NULL, "/" );
show(text, sizeof text);
printf(", wlist[%d] = \"%s\"\n", i, wlist[i]);
}
puts("---");
for ( i=0; i < WORD_COUNT-1; i++ )
{
strcat(wlist[i], "/");
show(text, sizeof text);
printf(", wlist[%d] = \"%s\"\n", i, wlist[i]);
}
return 0;
}
/* my output
/The/quick/brown/fox/jumped/over/the/lazy/red/dog0
---
/The0quick/brown/fox/jumped/over/the/lazy/red/dog0, wlist[0] = "The"
/The0quick0brown/fox/jumped/over/the/lazy/red/dog0, wlist[1] = "quick"
/The0quick0brown0fox/jumped/over/the/lazy/red/dog0, wlist[2] = "brown"
/The0quick0brown0fox0jumped/over/the/lazy/red/dog0, wlist[3] = "fox"
/The0quick0brown0fox0jumped0over/the/lazy/red/dog0, wlist[4] = "jumped"
/The0quick0brown0fox0jumped0over0the/lazy/red/dog0, wlist[5] = "over"
/The0quick0brown0fox0jumped0over0the0lazy/red/dog0, wlist[6] = "the"
/The0quick0brown0fox0jumped0over0the0lazy0red/dog0, wlist[7] = "lazy"
/The0quick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[8] = "red"
/The0quick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[9] = "dog"
---
/The/0uick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[0] = "The/"
/The//0ick0brown0fox0jumped0over0the0lazy0red0dog0, wlist[1] = "/"
/The//0ick0brown/0ox0jumped0over0the0lazy0red0dog0, wlist[2] = "brown/"
/The//0ick0brown//0x0jumped0over0the0lazy0red0dog0, wlist[3] = "/"
/The//0ick0brown//0x0jumped/0ver0the0lazy0red0dog0, wlist[4] = "jumped/"
/The//0ick0brown//0x0jumped//0er0the0lazy0red0dog0, wlist[5] = "/"
/The//0ick0brown//0x0jumped//0er0the/0azy0red0dog0, wlist[6] = "the/"
/The//0ick0brown//0x0jumped//0er0the//0zy0red0dog0, wlist[7] = "/"
/The//0ick0brown//0x0jumped//0er0the//0zy0red/0og0, wlist[8] = "red/"
*/
The nulls are shown as zero in the above.
The calls to strcat don't just write the character '/', they write the string "/", which is a '/' and a '\0'.