Solved

c language - get file name by path

Posted on 2013-12-11
3
395 Views
Last Modified: 2013-12-11
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
Comment
Question by:Johny12345
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
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

0
 

Author Comment

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

thanks
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39713042
Glad I could help! :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 101
Problem to ASCII 1 179
What's the Difference Between a VI, the Command Prompt and a Shell 7 108
C Programming - If Statement 8 77
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

777 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