hugheslin@hotmail.com (Hughes) wrote in message news:<49ca858c.0311121414.179f346b@posting.google. com>...[color=blue]
> Hi,
>
> I would like to get the filename from a folder by using C language.
> For example, in path /Users/abc/Desktop/xyz/ there is a file named
> "test.s" (file "test.s" is inside folder "xyz"). How should I write a
> routine in C to get the file name "test.s" ? Thanks a lot.[/color]
Assuming you mean "How do I extract the substring 'test.s' from the
string '/Users/abc/Desktop/xyz/test.s'", here's one approach:
#include <stdio.h>
#include <string.h>
char *file_from_path (char *pathname)
{
char *fname = NULL;
if (pathname)
{
fname = strrchr (pathname, '/') + 1;
}
return fname;
}
int main (void)
{
char pathname[] = "/Users/abc/Desktop/xyz/test.s";
char *fname = file_from_path (pathname);
printf ("path \"%s\", filename \"%s\"\n", pathname,
fname != NULL ? fname : "(null)");
return 0;
}
Read up on strrchr() for details.
If you're asking "How do I search for a particular file on my hard
drive", that's something that requires system-specific routines; C
does not provide high-level support for file system management.