Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   How to retrieve file name without path? (http://www.programmingforums.org/showthread.php?t=15715)

CPU Apr 27th, 2008 5:55 AM

How to retrieve file name without path?
 
GetModuleFileName() function gets filename and its full path.How to retrieve file name without path?

Ancient Dragon Apr 27th, 2008 5:58 AM

Re: How to retrieve file name without path?
 
just chop off the path and you have it.

CPU Apr 27th, 2008 6:13 AM

Re: How to retrieve file name without path?
 
how?

Jabo Apr 27th, 2008 6:24 AM

Re: How to retrieve file name without path?
 
get the index of the last / or \ and do a substring.

Sane Apr 27th, 2008 8:16 AM

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.

Ancient Dragon Apr 27th, 2008 4:28 PM

Re: How to retrieve file name without path?
 
Quote:

Originally Posted by CPU (Post 144408)
how?

You mean to tell me you are trying to write a windows program and know absolutely nothing about c strings and pointers? :icon_rolleyes:
:

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);


CPU Apr 29th, 2008 10:48 AM

Re: How to retrieve file name without path?
 
Quote:

Originally Posted by Ancient Dragon (Post 144425)
You mean to tell me you are trying to write a windows program and know absolutely nothing about c strings and pointers? :icon_rolleyes:
:

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.

lectricpharaoh Apr 30th, 2008 6:02 PM

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.

Sane Apr 30th, 2008 6:39 PM

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.


Ancient Dragon Apr 30th, 2008 7:36 PM

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.


All times are GMT -5. The time now is 12:46 PM.

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