c language - get file name by path

Posted on 2013-12-11
Last Modified: 2013-12-11

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:


Please guide.

Question by:Johny12345
  • 2
LVL 35

Accepted Solution

mccarl earned 500 total points
ID: 39713033
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


Author Comment

ID: 39713040
Yes, I figured it out but u helped me  :)

LVL 35

Expert Comment

ID: 39713042
Glad I could help! :)

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now