Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2008, 5:55 AM   #1
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
How to retrieve file name without path?

GetModuleFileName() function gets filename and its full path.How to retrieve file name without path?
CPU is offline   Reply With Quote
Old Apr 27th, 2008, 5:58 AM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 551
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: How to retrieve file name without path?

just chop off the path and you have it.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old Apr 27th, 2008, 6:13 AM   #3
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
Re: How to retrieve file name without path?

how?
CPU is offline   Reply With Quote
Old Apr 27th, 2008, 6:24 AM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 274
Rep Power: 2 Jabo is on a distinguished road
Re: How to retrieve file name without path?

get the index of the last / or \ and do a substring.
Jabo is offline   Reply With Quote
Old Apr 27th, 2008, 8:16 AM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,890
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How to retrieve file name without path?

You want the basename function. Since there is no standard implementation in C, you could use a prewritten implementation.

You generally don't want to write your own, since the format's not always so simple. There are standards, that prewritten implementations have covered.
Sane is offline   Reply With Quote
Old Apr 27th, 2008, 4:28 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 551
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: How to retrieve file name without path?

Quote:
Originally Posted by CPU View Post
how?
You mean to tell me you are trying to write a windows program and know absolutely nothing about c strings and pointers?
char basename[126] = {0};
char path[] = "c:\\Program Files\\My Directory\\MyProgram.exe";
char *ptr = strrchr(path,'\\');
if(ptr != NULL)
   strcpy(basename,ptr+1);

// Or, you could also do this:
strcpy(path, ptr+1);
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old Apr 29th, 2008, 10:48 AM   #7
CPU
Newbie
 
Join Date: Apr 2008
Posts: 15
Rep Power: 0 CPU is on a distinguished road
Re: How to retrieve file name without path?

Quote:
Originally Posted by Ancient Dragon View Post
You mean to tell me you are trying to write a windows program and know absolutely nothing about c strings and pointers?
char basename[126] = {0};
char path[] = "c:\\Program Files\\My Directory\\MyProgram.exe";
char *ptr = strrchr(path,'\\');
if(ptr != NULL)
   strcpy(basename,ptr+1);

// Or, you could also do this:
strcpy(path, ptr+1);
I wrote my own code but i want to learn a simple function can do same job which i dont know.But "Sane" answered my question.and IT IS NOT NECESSARY TO INSULT ME.

Last edited by CPU; Apr 29th, 2008 at 11:04 AM.
CPU is offline   Reply With Quote
Old Apr 30th, 2008, 6:02 PM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,051
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: How to retrieve file name without path?

Quote:
Originally Posted by CPU
IT IS NOT NECESSARY TO INSULT ME.
You weren't being insulted. Rather, Ancient Dragon was just surprised you'd be trying to write a Windows program without enough knowledge. You've got to start off small. Windows programming in plain C requires a detailed knowledge of pointers, structures, unions, typedefs, and function pointers, not to mention the Win32 API itself.

Let's face it; if converting "C:\somefolder\someprogram.exe" to "program.exe" is a serious obstacle for you, then your skills aren't yet sharp enough to write a non-trivial Windows program from scratch. This isn't an insult. It's a statement of fact. Try to focus on improving what you do know, and don't assume everyone offering opinions is trying to beat you down.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Apr 30th, 2008, 6:39 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,890
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How to retrieve file name without path?

Lectric, there's no indication that the OP could not accomplish that simple case in C.

He even said: "I wrote my own code but i want to learn a simple function can do same job."

So he even did write it, but he wanted to know if there's something tried-and-tested he can use.

Besides, there are standards for the basename of a file path. It's not always as clear-cut as "C:\somefolder\someprogram.exe" to "program.exe". Correct implementation of all standards must be followed. So the OP was correct, and rather smart, in asking about whether or not there's an existing function for this, instead of writing his own.

  
   (1) Strip trailing slashes.
       If string is //, it is implementation-dependent whether 
       steps (2) to (5) are skipped or processed. 

   (2) If string consists entirely of slash characters, string shall be
       set to a single slash character.  In this case, skip steps (3)
       through (5).

   (3) If there are any trailing slash characters in string, they
       shall be removed. 

   (4) If there are any slash characters remaining in string, the prefix
       of string up to an including the last slash character in string
       shall be removed. 

   (5) If the suffix operand is present, is not identical to the
       characters remaining in string, and is identical to a suffix
       of the characters remaining in string, the suffix suffix
       shall be removed from string.  Otherwise, string shall not be
       modified by this step.

Last edited by Sane; Apr 30th, 2008 at 6:52 PM.
Sane is offline   Reply With Quote
Old Apr 30th, 2008, 7:36 PM   #10
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 551
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: How to retrieve file name without path?

>>Besides, there are standards for the basename of a file path
Sorry, Sane, but there is no such a standard. C languages specificiations make no such statements. The links you posted earlier are only POSIX and do not apply to MS-Windows or MS-DOS. Don't know about other operating systems. So, to say there are standards is just simply misinformation.

GetModuleFileName() as referenced in the OP is a MS-Windows win32 api function, not *nix.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
File path in Mac OS X poopman Python 3 Feb 2nd, 2007 8:59 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC