LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-22-2009, 08:55 AM   #1
htabesh
LQ Newbie
 
Registered: May 2006
Location: Iran-Tehran
Distribution: Enterprise RedHat
Posts: 25

Rep: Reputation: 16
Question How To Obtain File Name in C?


How Can I Obtain file name (file name and extension) from a path?
for example:

path = /var/www/index.html
Code:
FILE *fp=fopen("/var/www/index.html","w");
I need "index.html"

thanks.
 
Old 06-22-2009, 09:05 AM   #2
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
Code:
#include <strings.h>
#include <string.h>

int last_slash_pos = rindex( path, '/')
char* filename = strdup( &path[ last_slash_pos+1])
 
Old 06-22-2009, 09:25 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by kellinwood View Post
Code:
int last_slash_pos = rindex[/b]( path, '/')
No. rindex() does not return int, but char*.
Also, strrchr() does the same thing but is better supported along different unix-like OS's.

If you are not changing the string path or filename you can just do:
Code:
#include <string.h>
#include <stdio.h>

int main()
{
    char *path;
    char *filename;

    path = "/var/www/index.html";
    filename = strrchr(path, '/') + 1;
    printf("Filename: %s\n", filename);
    return 0;
}
Only if you do need to change the string path or filename, you can duplicate the string filename with strdup(). In that case, don't forget to free() the dynamically allocated memory.
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *path;
    char *filename;

    path = "/var/www/index.html";
    filename = strdup(strrchr(path, '/') + 1);
    printf("Filename: %s\n", filename);
    free(filename);
    return 0;
}
 
Old 06-23-2009, 05:46 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 637

Rep: Reputation: 140Reputation: 140
Dirname, basename
?
 
Old 06-23-2009, 07:31 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by AnanthaP View Post
Dirname, basename
?
Yep, that would've been my first instinct too. But it may not be as portable as the other solutions presented.

In case the OP is working under a Linux-only environment, then here's the solution using basename():

Code:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <string.h>
#include <stdio.h>

int main()
{
  const char* longpath = "/var/www/index.html";
  const char* filename = basename(longpath);

  printf("filename = %s\n", filename);

  return 0;
}
 
  


Reply

Tags
string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible to obtain C code from module (file .o)??? webquinty Linux - Newbie 5 11-03-2008 05:32 AM
Obtain file system info with df AQG Solaris / OpenSolaris 1 08-02-2007 08:18 PM
Not able to obtain file size skie_knite007 Programming 5 08-08-2005 12:10 PM
how to obtain missing file? okok Linux - Newbie 2 02-23-2002 03:35 AM


All times are GMT -5. The time now is 06:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration