basename in C++

P

PengYu.UT

libgen.h has the basename command. But it is not a C++ head file.
Is there any C++ head file have a function or a class doing the same
thing?


Thanks,
Peng
 
V

Victor Bazarov

libgen.h has the basename command. But it is not a C++ head file.
Is there any C++ head file have a function or a class doing the same
thing?

Which thing is that?

V
 
R

Rolf Magnus

Victor said:
Which thing is that?

basename is part of the POSIX standard. On my system, the manual page for it
says:

DIRNAME(3) Linux Programmer’s Manual

NAME
dirname, basename - Parse pathname components

SYNOPSIS
#include <libgen.h>

char *dirname(char *path);
char *basename(char *path);

DESCRIPTION
Warning: there are two different functions basename() - see below.
The functions dirname() and basename() break a null-terminated
pathname string into directory and filename components. In the
usual case, dirname() returns the string up to, but not including,
the final ’/’, and basename() returns the component following the
final ’/’. Trailing ’/’ characters are not counted as part of the
pathname.


So the answer is: There is no standard C++ function to do that. In standard
C++, there is no such thing as directories, so there no support whatsoever
for it.
 
M

Michal Nazarewicz

libgen.h has the basename command. But it is not a C++ head file.
Is there any C++ head file have a function or a class doing the same
thing?

foo = basename(bar) works pretty much like:

#v+
foo = strrchr(bar, '/');
foo = foo ? foo + 1 : bar;
#v-
 
J

Jens Theisen

libgen.h has the basename command. But it is not a C++ head file.
Is there any C++ head file have a function or a class doing the same
thing?

If you're just looking for basename, you should probably use the posix
thing.

There is, however, a boost library for this stuff (filesystem). Note
that they call basename leaf, and their basename has something to do
with file extensions.

Regards,

Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top