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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 36

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 36

Expert Comment

ID: 39713042
Glad I could help! :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them in the C programming language.

734 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