Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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