![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
How to retrieve file name without path?
GetModuleFileName() function gets filename and its full path.How to retrieve file name without path?
|
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
Re: How to retrieve file name without path?
just chop off the path and you have it.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
Re: How to retrieve file name without path?
how?
|
|
|
|
|
|
#4 |
|
Not a user?
Join Date: Sep 2007
Posts: 232
Rep Power: 1
![]() |
Re: How to retrieve file name without path?
get the index of the last / or \ and do a substring.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#6 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
Re: How to retrieve file name without path?
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 Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0
![]() |
Re: How to retrieve file name without path?
Quote:
Last edited by CPU; Apr 29th, 2008 at 11:04 AM. |
|
|
|
|
|
|
#8 | |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 895
Rep Power: 4
![]() |
Re: How to retrieve file name without path?
Quote:
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.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
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.
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Apr 30th, 2008 at 6:52 PM. |
|
|
|
|
|
#10 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
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.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |