Talk About Network

Google





Programming > C - C++ Learning > Extract directo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 4191 of 4400
Post > Topic >>

Extract directory from filename

by Pranav Negandhi <pranav@[EMAIL PROTECTED] > Jul 4, 2008 at 12:36 AM

I need to extract the directory ****tion from a filename string (e.g. 
c:\foo\bar.txt).

I came across the following splitpath function through a bit of 
searching around. I figured that the second and third parameters are 
pointers to character arrays which will be filled when the function 
returns. But I can't understand how those pointers have to be passed to 
it. I tried this which fails -

void main()
{
	char **dirpart;
	char **filepart;
	splitpath("c:\\foo\\bar.txt", dirpart, filepart);
}

int splitpath(char *path, char **dirpart, char **filepart)
{
     static char	DOT[] = ".";
     char	*dpart;
     char	*fpart;

     if ((fpart = strrchr(path, '/')) == NULL)
	{
		if ((dpart = strdup(DOT)) == NULL)
			return -1;

		if ((fpart = strdup(path)) == NULL)
		{
			free(dpart);
			return -1;
		}
     }
     else
	{
		if ((dpart = strdup(path)) == NULL)
			return -1;

		dpart[fpart - path] = '0';
		if ((fpart = strdup(++fpart)) == NULL)
		{
			free(dpart);
			return -1;
		}
     }

     *dirpart = dpart;
     *filepart = fpart;

     return 0;
}

Somebody shed some light here?

..p
 




 5 Posts in Topic:
Extract directory from filename
Pranav Negandhi <prana  2008-07-04 00:36:19 
Re: Extract directory from filename
ppi <vodoom@[EMAIL PRO  2008-07-03 13:15:06 
Re: Extract directory from filename
Richard Heathfield <rj  2008-07-04 05:59:54 
Re: Extract directory from filename
Richard Heathfield <rj  2008-07-04 05:59:14 
Re: Extract directory from filename
Pranav Negandhi <prana  2008-07-05 00:45:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 5:35:14 PST 2009.