• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

c language - get file name by path

Hi,

I am trying to get file name but it is not working for me. Here is my code:

char *file_from_path (char *pathname)
{
char *fname = NULL;

if (pathname)
{
fname = strrchr (pathname, '/') + 1;
}

return fname;
}

Open in new window


	char *pathname =argv[3];
char *fname = file_from_path (pathname);
printf(fname);  // application crash

Open in new window


The path is like typical windows path like below:

c:\folder1\sub\image\001.jpg

Please guide.

Thanks
0
Johny12345
Asked:
Johny12345
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Your path contains "backslash" \ as the separator but you are searching for "forward slash" /

Try this line inplace of line 7 above...
fname = strrchr (pathname, '\\') + 1;

Open in new window

0
 
Johny12345Author Commented:
Yes, I figured it out but u helped me  :)

thanks
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Glad I could help! :)
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now