Hello--I am trying to get a program to parse a path, filename, and extension. Here's what I have so far:
The program outputs:Code:
#include <stdio.h>
#include <stdlib.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[] = "C:/Users/Michael/Desktop/File Directory/example.doc";
char *fname = file_from_path(pathname);
printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
printf("\n");
system("pause");
return 0;
}
path: "C:/Users/Michael/Desktop/File Directory/example.doc"
filename: "example.doc"
What I'd like to add is a part where it would output
path:
filename:
extension: ".doc"
Is this possible using the code that I have set up? Thanks in advance for any help.
Michael