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
Comment Utility
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

Comment Utility
Yes, I figured it out but u helped me  :)

LVL 35

Expert Comment

Comment Utility
Glad I could help! :)

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

772 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

11 Experts available now in Live!

Get 1:1 Help Now