By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Separating directory from file name

P: n/a
If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

Thanks,
Joe
Nov 14 '05 #1
Share this Question
Share on Google+
39 Replies


P: n/a
Joe Laughlin wrote:
If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.


Homework question BEGONE!

[hint cuz I want you to fail at your job by being unable to solve
trivial problems yourself....: scan backwards in the string for the first /]

Tom
Nov 14 '05 #2

P: n/a
Tom St Denis wrote:
Joe Laughlin wrote:
If I have a character array with
"/some/file/directory/file_name", are there any
functions / libraries that I could use to separate the
directory ("some/file/directory") from the file name
("file_name").

I looked at sscanf(), but that didn't seem to do what I
wanted.


Homework question BEGONE!

[hint cuz I want you to fail at your job by being unable
to solve
trivial problems yourself....: scan backwards in the
string for the first /]

Tom


I'm not familar with C character array processing, sorry. Again, are there
any standard libraries that would let me scan backwards, or do I need to do
this manually?
Nov 14 '05 #3

P: n/a
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
Tom St Denis wrote:
Joe Laughlin wrote:
If I have a character array with
"/some/file/directory/file_name", are there any
functions / libraries that I could use to separate the
directory ("some/file/directory") from the file name
("file_name").

I looked at sscanf(), but that didn't seem to do what I
wanted.
Homework question BEGONE!

[hint cuz I want you to fail at your job by being unable
to solve
trivial problems yourself....: scan backwards in the
string for the first /]

Tom


I'm not familar with C character array processing, sorry. Again, are

there any standard libraries that would let me scan backwards, or do I need to do this manually?


There may be a library function that'll do this for you, but it's quite
quick to do it manually too. See the example below:
void extractfilename(const char *path, char *filename) {
int i,c=0;

/* put the highest value for n in path[n] into i (if we ignore the \0)
*/
i=strlen(path)-1;

/* itterate backwards through the array decreasing i until we get to a
'/' */
while (i>0) {
if (path[i] == '/')
break;
i--;
}

/* itterate forwards through the array starting from the element after
after the '/' and dump the remaining characters into filename */
while (i<strlen(path)) {
filename[c++]=path[++i];
}
}

I wrote this to test my own knowledge more than anything, comments invited.
A better version of the function would take the size of the destination
array as an argument to avoid buffer overflows but I left this out for
simplicity. Code compiles without warnings in gcc with -Wall -pedantic.
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/
Nov 14 '05 #4

P: n/a
"Kieran Simkin" <ki****@digital-crocus.com> wrote in message
news:Cuavc.582$S83.270@newsfe4-gui...
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
Tom St Denis wrote:
Joe Laughlin wrote:
> If I have a character array with
> "/some/file/directory/file_name", are there any
> functions / libraries that I could use to separate the
> directory ("some/file/directory") from the file name
> ("file_name").
>
> I looked at sscanf(), but that didn't seem to do what I
> wanted.

Homework question BEGONE!

[hint cuz I want you to fail at your job by being unable
to solve
trivial problems yourself....: scan backwards in the
string for the first /]

Tom
I'm not familar with C character array processing, sorry. Again, are

there
any standard libraries that would let me scan backwards, or do I need to

do
this manually?


There may be a library function that'll do this for you, but it's quite
quick to do it manually too. See the example below:


Forgot to paste the #include <string.h> here.. Oops.
void extractfilename(const char *path, char *filename) {
int i,c=0;

/* put the highest value for n in path[n] into i (if we ignore the \0)
*/
i=strlen(path)-1;

/* itterate backwards through the array decreasing i until we get to a
'/' */
while (i>0) {
if (path[i] == '/')
break;
i--;
}

/* itterate forwards through the array starting from the element after
after the '/' and dump the remaining characters into filename */
while (i<strlen(path)) {
filename[c++]=path[++i];
}
}

I wrote this to test my own knowledge more than anything, comments invited. A better version of the function would take the size of the destination
array as an argument to avoid buffer overflows but I left this out for
simplicity. Code compiles without warnings in gcc with -Wall -pedantic.
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/

Nov 14 '05 #5

P: n/a
"Joe Laughlin" <Jo***************@boeing.com> wrote:
# Tom St Denis wrote:
# > Joe Laughlin wrote:
# >> If I have a character array with
# >> "/some/file/directory/file_name", are there any
# >> functions / libraries that I could use to separate the
# >> directory ("some/file/directory") from the file name
# >> ("file_name").

The problem as stated is not answerable without specifying the
operating system. NOS and TSOS didn't even have directories. On Mac System
7, that is a single file name.

# I'm not familar with C character array processing, sorry. Again, are there
# any standard libraries that would let me scan backwards, or do I need to do
# this manually?

If you have a unix available, try
manpage 3 string
and then follow the see-alsos.

You can also browse
http://www.squarebox.co.uk/cgi-squar.../man3/string.3
and follow the links.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Nov 14 '05 #6

P: n/a
On Tue, 1 Jun 2004 22:54:49 GMT, "Joe Laughlin"
<Jo***************@boeing.com> wrote in comp.lang.c:
If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

Thanks,
Joe


While the format of path names, or even file names, are properties of
the operating system and not specified by C, you have a string issue
here that the C library can easily handle.

Look up the function strrchr() -- note two 'r' characters in the
middle, not just one. That will return a pointer to the rightmost '/'
(or any other) character in your string, or NULL if the character is
not present.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

P: n/a
On Tue, 1 Jun 2004 22:54:49 GMT, "Joe Laughlin"
<Jo***************@boeing.com> wrote:
If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

There is a function in the standard library, for which string.h
contains the prototype, that you can use to determine where the last
'/' occurs in your array. It should not be hard for you to find it in
your text or reference and then to figure out how to use this
information to perform the separation you want.
<<Remove the del for email>>
Nov 14 '05 #8

P: n/a
"Kieran Simkin" <ki****@digital-crocus.com> writes:
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
> Joe Laughlin wrote:
>> If I have a character array with
>> "/some/file/directory/file_name", are there any
>> functions / libraries that I could use to separate the
>> directory ("some/file/directory") from the file name
>> ("file_name").
>>
>> I looked at sscanf(), but that didn't seem to do what I
>> wanted.
I'm not familar with C character array processing, sorry. Again, are
there any standard libraries that would let me scan backwards, or do
I need to do this manually?


There may be a library function that'll do this for you, but it's quite
quick to do it manually too. See the example below:
void extractfilename(const char *path, char *filename) {
int i,c=0;

/* put the highest value for n in path[n] into i (if we ignore
the \0) */
i=strlen(path)-1;

/* itterate backwards through the array decreasing i until we get
to a '/' */
while (i>0) {
if (path[i] == '/')
break;
i--;
}

/* itterate forwards through the array starting from the element
after after the '/' and dump the remaining characters into
filename */
while (i<strlen(path)) {
filename[c++]=path[++i];
}
}

I wrote this to test my own knowledge more than anything, comments
invited. A better version of the function would take the size of the
destination array as an argument to avoid buffer overflows but I left
this out for simplicity. Code compiles without warnings in gcc with
-Wall -pedantic.


Three comments:

- `int' does not necessarily have a large enough range to represent
the size of an object. Better use `size_t', the type returned
by `strlen'. However, if you just change `int' to `size_t' (an
/unsigned/ type), your algorithm won't work anymore - think about
what happens when `path' is an empty string.

- Your algorithm is inefficient in that it first scans the complete
string to find its end (strlen), and then scans part of the string
/again/ to find the last slash. This can be avoided by scanning the
string once and in the process remembering the last slash encountered.

- It is also inefficient to call `strlen (path)' in the while loop.
Unless the compiler recognizes that it can optimize this by moving the
`strlen' call out of the loop, the string will be scanned once for
every iteration!
#include <stdio.h>

/* Undefined behavior if `path' is a null pointer, or if `filename'
doesn't point to a large enough buffer. Return `filename'. */
char *extractfilename (const char *path, char *const filename)
{
/* Pointer the first character that should be copied. */
const char *src = path;

/* Pointer to the buffer where the filename should be copied. */
char *dst = filename;

while (*path != '\0')
{
/* Increment `path' and look for a slash. */
if (*path++ == '/')
{
/* If a slash has been found, `path' has already been
incremented, so that `src' points one past the slash. */
src = path;
}
}

/* Copy remaining characters. */
while (*dst++ = *src++)
continue;

return filename;
}

int main (void)
{
const char *test1 = "/path/and/filename";
const char *test2 = "filename_without_path";
const char *test3 = "/trailing/slash/";
const char *test4 = "";
char buffer [22];

printf ("test1: \"%s\" -> \"%s\"\n", test1, extractfilename (test1, buffer));
printf ("test2: \"%s\" -> \"%s\"\n", test2, extractfilename (test2, buffer));
printf ("test3: \"%s\" -> \"%s\"\n", test3, extractfilename (test3, buffer));
printf ("test4: \"%s\" -> \"%s\"\n", test4, extractfilename (test4, buffer));
return 0;
}
Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #9

P: n/a
In <10*************@corp.supernews.com> SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango-dot-charlie-oscar-mike.fake.org> writes:
"Joe Laughlin" <Jo***************@boeing.com> wrote:
# Tom St Denis wrote:
# > Joe Laughlin wrote:
# >> If I have a character array with
# >> "/some/file/directory/file_name", are there any
# >> functions / libraries that I could use to separate the
# >> directory ("some/file/directory") from the file name
# >> ("file_name").

The problem as stated is not answerable without specifying the
operating system. NOS and TSOS didn't even have directories. On Mac System
7, that is a single file name.
The problem, as stated, came with a concrete example that makes it
perfectly answerable.
# I'm not familar with C character array processing, sorry. Again, are there
# any standard libraries that would let me scan backwards, or do I need to do
# this manually?

If you have a unix available, try
manpage 3 string
and then follow the see-alsos.


All the advice provided by this command on the average Unix system is:

fangorn:~ 11> manpage 3 string
manpage: Command not found.

Not particularly helpful...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

P: n/a
In <Hy********@news.boeing.com> "Joe Laughlin" <Jo***************@boeing.com> writes:
If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.


If you want to deal with strings in C, you *really* need to study the
description of <string.h> in your favourite C book.

It is obvious that what you need is the address of the last '/' character
in your character array. The standard library function that finds the
last occurrence of a character in a string is strrchr. If the function
fails to find it, the whole character array must be considered the file
name.

So, if you can overwrite your character array, after

char *fn = strrchr(array, '/');
if (fn != NULL) *fn++ = 0;
else fn = array;

fn will point to the file name and, if fn != array, array will contain the
path name.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

P: n/a
Martin Dickopp wrote:
.... snip ...
Your algorithm is inefficient in that it first scans the complete
string to find its end (strlen), and then scans part of the string
/again/ to find the last slash. This can be avoided by scanning the
string once and in the process remembering the last slash encountered.


Not necessarily so. The comparisons made in the combined scan can
be much less efficient, and necessarily encompass the complete
string. The second half of the separated scan starts from the
end, and may be much shorter.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #12

P: n/a
On Wed, 2 Jun 2004 00:15:08 GMT, "Joe Laughlin"
<Jo***************@boeing.com> wrote:
Tom St Denis wrote:
Joe Laughlin wrote:
If I have a character array with
"/some/file/directory/file_name", are there any
functions / libraries that I could use to separate the
directory ("some/file/directory") from the file name
("file_name").

I looked at sscanf(), but that didn't seem to do what I
wanted.


Homework question BEGONE!

[hint cuz I want you to fail at your job by being unable
to solve
trivial problems yourself....: scan backwards in the
string for the first /]

Tom


I'm not familar with C character array processing, sorry. Again, are there
any standard libraries that would let me scan backwards, or do I need to do
this manually?

If this is a homework question, you need to have a talk with your
instructor. If it isn't homework, I think you need to hire a
programmer.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #13

P: n/a
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
If I have a character array with "/some/file/directory/file_name", are there any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

I don't know about any standard C answer to the stated problem,
but your example might imply UNIX pathname syntax (although
also acceptend on Windoze, though not on command line). So even
standard C answer (e.g. strrchr(), as suggested) may be platform specific.
If you are on UNIX, there should be basename(3C). For details
see e.g. here:
http://docs.sun.com/db/doc/805-3175/...name-3c-indx-1

My apologies to the group for an OT answer. But in this case I consider
it worth giving. It's not portable, but neither are some others. I've
already
forgotten what path looked like on VAX. Original problem was to extract
file name from the full path name, not to find last '/'.
Nov 14 '05 #14

P: n/a
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Martin Dickopp wrote:

... snip ...

Your algorithm is inefficient in that it first scans the complete
string to find its end (strlen), and then scans part of the string
/again/ to find the last slash. This can be avoided by scanning the
string once and in the process remembering the last slash encountered.


Not necessarily so. The comparisons made in the combined scan can
be much less efficient, and necessarily encompass the complete
string. The second half of the separated scan starts from the
end, and may be much shorter.


I'm afraid I have to agree with Martin. (Ouch)

All algorithms I have seen in c that "start at the end" of a string,
actually scan forwards to a '/0'. If this were Pascal, where the length is
given and you could jump to the end of the string with a memory read (and a
256 byte size limit) then you might be correct. But in C you are better off
storing the slashes as you find them and only scan once through the string.
I have never found strrchr() to be very useful, as I can build a loop that
does what I want better than the standard function.

--
Mabden
Nov 14 '05 #15

P: n/a
"Mabden" <ma****@sbcglobal.net> writes:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Martin Dickopp wrote:
>

... snip ...
>
> Your algorithm is inefficient in that it first scans the complete
> string to find its end (strlen), and then scans part of the string
> /again/ to find the last slash. This can be avoided by scanning the
> string once and in the process remembering the last slash encountered.


Not necessarily so. The comparisons made in the combined scan can
be much less efficient, and necessarily encompass the complete
string. The second half of the separated scan starts from the
end, and may be much shorter.


I'm afraid I have to agree with Martin. (Ouch)


Chuck does have a point. I think the important message is that my
implementation is not guaranteed to be more efficient than the original
one, but neither vice versa. If it matters, some benchmarking for
typical input strings must be done.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #16

P: n/a
Hey, Martin! I thought you killfiled me over some comedy I posted once! Did
you grow a sensual humor?

--
Mabden
Nov 14 '05 #17

P: n/a
Mabden wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message
Martin Dickopp wrote:

... snip ...

Your algorithm is inefficient in that it first scans the complete
string to find its end (strlen), and then scans part of the string
/again/ to find the last slash. This can be avoided by scanning the
string once and in the process remembering the last slash encountered.


Not necessarily so. The comparisons made in the combined scan can
be much less efficient, and necessarily encompass the complete
string. The second half of the separated scan starts from the
end, and may be much shorter.


I'm afraid I have to agree with Martin. (Ouch)

All algorithms I have seen in c that "start at the end" of a string,
actually scan forwards to a '/0'. If this were Pascal, where the length is
given and you could jump to the end of the string with a memory read (and a
256 byte size limit) then you might be correct. But in C you are better off
storing the slashes as you find them and only scan once through the string.
I have never found strrchr() to be very useful, as I can build a loop that
does what I want better than the standard function.


The difference lies in the complexity of the initial scan. The
two scan looks something like:

p = ptr;
while (*p++) continue;
p--;
while (('/' != *p) && (p > ptr)) p--;

while the complex single scan is something like:

pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #18

P: n/a
CBFalconer <cb********@yahoo.com> writes:

The difference lies in the complexity of the initial scan. The
two scan looks something like: p = ptr;
while (*p++) continue;
p--;
while (('/' != *p) && (p > ptr)) p--; while the complex single scan is something like: pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}

Is there something inherently wrong with using standard functions, here,
or is this just a macho contest? Such as:

argv0 = (argv0 = strrchr(argv[0],'/')) ? argv0+1 : argv[0];

and assuming that the standard functions will be reasonably implemented
and tested?

__________________________________________________ _____________________________
Dr Chris McDonald EMAIL: ch***@csse.uwa.edu.au
School of Computer Science & Software Engineering
The University of Western Australia WWW: http://www.csse.uwa.edu.au/~chris
Crawley, Western Australia, 6009 PH: +61 8 6488 2533, FAX: +61 8 6488 1089
Nov 14 '05 #19

P: n/a
In <4n*******************@newssvr25.news.prodigy.co m> "Mabden" <ma****@sbcglobal.net> writes:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Martin Dickopp wrote:
> ... snip ...
>
> Your algorithm is inefficient in that it first scans the complete
> string to find its end (strlen), and then scans part of the string
> /again/ to find the last slash. This can be avoided by scanning the
> string once and in the process remembering the last slash encountered.


Not necessarily so. The comparisons made in the combined scan can
be much less efficient, and necessarily encompass the complete
string. The second half of the separated scan starts from the
end, and may be much shorter.


I'm afraid I have to agree with Martin. (Ouch)

All algorithms I have seen in c that "start at the end" of a string,
actually scan forwards to a '/0'. If this were Pascal, where the length is
given and you could jump to the end of the string with a memory read (and a
256 byte size limit) then you might be correct. But in C you are better off

^^^^^^^^^^^^^^^^^^^^^^^^^^^storing the slashes as you find them and only scan once through the string. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^I have never found strrchr() to be very useful, as I can build a loop that
does what I want better than the standard function.


What makes you think that strrchr() doesn't work exactly as you have
described above? All the generic C implementations of strrchr() I am
familiar with do a single scan, until they find the null character and
"remember" the last address of the target character.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #20

P: n/a
Mabden wrote:
Did you grow a sensual humor?


"sensual humor" is that another old-timer joke?

Nov 14 '05 #21

P: n/a
In <NR***************@news04.bloor.is.net.cable.roger s.com> "nobody" <no****@nowhere.non> writes:
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
If I have a character array with "/some/file/directory/file_name", arethere
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

I don't know about any standard C answer to the stated problem,
but your example might imply UNIX pathname syntax (although
also acceptend on Windoze, though not on command line).


Even on command line, on modern Windows versions. It's only the good old
COMMAND.COM (R.I.P.) that had a problem with them.
So even
standard C answer (e.g. strrchr(), as suggested) may be platform specific.
If you are on UNIX, there should be basename(3C). For details
see e.g. here:
http://docs.sun.com/db/doc/805-3175/...name-3c-indx-1

My apologies to the group for an OT answer. But in this case I consider
it worth giving. It's not portable, but neither are some others. I've
already
forgotten what path looked like on VAX.
If the VAX was running any flavour of Unix, as quite a lot of them
actually did... ;-)

Just like on Windows, C on VMS was/is perfectly happy with Unix-like path
specifications, dua0:[foo.bar.baz]hello.c being also accepted as
/dua0/foo/bar/baz/hello.c . IIRC, even remote file specifications like
remotehost::dua0:[foo.bar.baz]hello.c could be translated into a Unix-like
path specification: //remotehost/dua0/foo/bar/baz/hello.c . This was also
the native path syntax on Domain/OS, where the // standed for the internet
root directory and the first single / for the host root directory.
Original problem was to extract
file name from the full path name, not to find last '/'.


If you really think that, you're reading impaired. The description of the
original problem is still included at the top of the article and it seems
to have everything to do with finding the last '/' character. And since
this can be trivially achieved in standard C, there is no point in
recommending anything else, as long as you do it in comp.lang.c.

Consider the cases when you have to split a Unix path specification on
a non-Unix platform.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #22

P: n/a
nobody wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:Hy********@news.boeing.com...
If I have a character array with
"/some/file/directory/file_name", are there any
functions / libraries that I could use to separate the
directory ("some/file/directory") from the file name
("file_name").

I looked at sscanf(), but that didn't seem to do what I
wanted.

I don't know about any standard C answer to the stated
problem,
but your example might imply UNIX pathname syntax
(although
also acceptend on Windoze, though not on command line).
So even
standard C answer (e.g. strrchr(), as suggested) may be
platform specific. If you are on UNIX, there should be
basename(3C). For details
see e.g. here:
http://docs.sun.com/db/doc/805-3175/...name-3c-indx-1

My apologies to the group for an OT answer. But in this
case I consider it worth giving. It's not portable, but
neither are some others. I've already
forgotten what path looked like on VAX. Original problem
was to extract file name from the full path name, not to
find last '/'.


I found what I really wanted... basename() and dirname(). Non-standard C
functions, but perfectly fine for my usage.
Nov 14 '05 #23

P: n/a
Chris McDonald <ch***@csse.uwa.edu.au> wrote:
CBFalconer <cb********@yahoo.com> writes:
The difference lies in the complexity of the initial scan. The
two scan looks something like:

p = ptr;
while (*p++) continue;
p--;
while (('/' != *p) && (p > ptr)) p--;

while the complex single scan is something like:

pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}
Is there something inherently wrong with using standard functions, here,


On the contrary. Alas, it seems to be part of every programmer's
learning curve to, at some point, think he's 'fit' enough to
'outsmart' the library implementer. Most programmers go past this
period very quickly, when they notice they fail miserably (and yes,
I know CBF has moved way beyond this phase many years ago :).
or is this just a macho contest?


Likely.... ;-)

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #24

P: n/a
"Mabden" <ma****@sbcglobal.net> writes:
Hey, Martin! I thought you killfiled me over some comedy I posted
once! Did you grow a sensual humor?


In that case, you presumably mistake me for somebody else. I rarely
killfile people (less often than once a year), and when I do that, I
never publically announce it.

I occasionally point out to people that a certain behaviour is likely
to get them killfilled (not necessarily by me). However, unless the
behavior is extremely offending, I instantly forget about it, and
therefore don't recognize such people when they come back and behave
better. Whether I ever have complained about /your/ behavior, I don't
recall.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #25

P: n/a
Chris McDonald <ch***@csse.uwa.edu.au> writes:
Is there something inherently wrong with using standard functions,
here, or is this just a macho contest?


Short answer: It's just a macho contest.

Longer answer: A few postings up, somebody pointed out that while the
problem can be easily solved with standard library functions, it is a
good exercise for a beginner to solve it without library functions.
He also posted an example implementation and invited comments.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #26

P: n/a
"Grumble" <a@b.c> wrote in message news:c9**********@news-rocq.inria.fr...
Mabden wrote:
Did you grow a sensual humor?


"sensual humor" is that another old-timer joke?


Nope. Just a phrase that popped into my head. Of course, it was more amusing
at 1am after a few...

--
Mabden
Nov 14 '05 #27

P: n/a
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9***********@sunnews.cern.ch...
In <4n*******************@newssvr25.news.prodigy.co m> "Mabden" <ma****@sbcglobal.net> writes:

All algorithms I have seen in c that "start at the end" of a string,
actually scan forwards to a '/0'. If this were Pascal, where the length isgiven and you could jump to the end of the string with a memory read (and a256 byte size limit) then you might be correct. But in C you are better off ^^^^^^^^^^^^^^^^^^^^^^^^^^^storing the slashes as you find them and only scan once through the string.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^I have never found strrchr() to be very useful, as I can build a loop thatdoes what I want better than the standard function.


What makes you think that strrchr() doesn't work exactly as you have
described above? All the generic C implementations of strrchr() I am
familiar with do a single scan, until they find the null character and
"remember" the last address of the target character.


You are right, Dan, I mixed up what I was saying. What I meant was that if
you take the strlen() of a string, add it to a pointer, and then move
backwards through the string, you are scanning twice.

I didn't mean to say the strchr() itself was a poor implementation -
although I clearly did say that.

I have to agree with whoever said that using common library functions is a
Good Thing, as everyone understands (or can look up) their function.

But if the string parsing is more complicated, one can just as easily write
a loop that scans through the string in one pass and sets a few pointers.

I guess I don't have a good point here, except that rippling through a
string is as easy in a loop as it is to call a function, and if you are
going to the end of the string only to come backwards then maybe you could
re-think what you are doing and make it a one-pass algorithm.

--
Mabden
Nov 14 '05 #28

P: n/a
kal
CBFalconer <cb********@yahoo.com> wrote in message news:<40***************@yahoo.com>...
while the complex single scan is something like:

pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}


The following may also work, if somewhat differently.

pp = p = ptr;
while (*p) {
if ('/' == *(p++)) pp = p;
}
Nov 14 '05 #29

P: n/a
kal wrote:
CBFalconer <cb********@yahoo.com> wrote:
while the complex single scan is something like:

pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}


The following may also work, if somewhat differently.

pp = p = ptr;
while (*p) {
if ('/' == *(p++)) pp = p;
}


It will have a one-off error.

Chuck, beating macho breast loudly.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #30

P: n/a
CBFalconer <cb********@yahoo.com> wrote:
kal wrote:
CBFalconer <cb********@yahoo.com> wrote:
while the complex single scan is something like:

pp = NULL; p = ptr;
while (*p) {
if ('/' == *p) pp = p;
p++;
}
The following may also work, if somewhat differently.

pp = p = ptr;
while (*p) {
if ('/' == *(p++)) pp = p;
}


It will have a one-off error.


Quite. But interestingly, it might be a perfect solution for
OP's original task ("Separating directory from file name"), in
that it returns a pointer to the character that immediately
follows the last occurrence of '/'.
Chuck, beating macho breast loudly.


Don't hurt yourself too bad... ;D

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #31

P: n/a
When posting my code I was aware of strrchr() and wasn't under any delusions
of being able to beat the standard libs. It was more just a programming
exercise for myself. It turned out I actually needed to do something similar
for a project I'm working on today.

I needed a function to take a unix pathname like "/home/foo/dir/subdir/" and
create any necessary directories that didn't already exist in the path. For
example /home/foo may exist, but dir and dir/subdir might not. Here's the
code I came up with: (only non-standard functions here are mkdir() and
direxists() and they're not really part of the text processing algorithm).

int mkrdir (const char *dir, const int mode) {
char *tdir,*pnam;
/* ideally this strlen() would not be necessary, but I didn't want
to use a fixed buf */
pnam = malloc(strlen(dir)+1);
if (pnam == NULL) {
return 1;
}
tdir=pnam;
while(*dir != '\0') {
if (*dir == '/') {
*(tdir+1)='\0';
if (strcmp(pnam,"")!=0) {
if (!direxists(pnam)) {
mkdir(pnam,mode);
}
}
}
*(tdir++)=*(dir++);
}
*tdir='\0';
if (*(tdir-1) != '/') {
mkdir(pnam,mode);
}
tdir=NULL;
free(pnam);
return 0;
}
--
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:cu*************@zero-based.org...
Chris McDonald <ch***@csse.uwa.edu.au> writes:
Is there something inherently wrong with using standard functions,
here, or is this just a macho contest?


Short answer: It's just a macho contest.

Longer answer: A few postings up, somebody pointed out that while the
problem can be easily solved with standard library functions, it is a
good exercise for a beginner to solve it without library functions.
He also posted an example implementation and invited comments.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/

Nov 14 '05 #32

P: n/a
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9***********@sunnews.cern.ch...
In <NR***************@news04.bloor.is.net.cable.roger s.com> "nobody" <no****@nowhere.non> writes:
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
If I have a character array with "/some/file/directory/file_name", arethere
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

Even on command line, on modern Windows versions. It's only the good old
COMMAND.COM (R.I.P.) that had a problem with them.


Maybe that depends on definition of "modern". It doesn't work on NT 4.0
and I believe neither on 2K (don't have one on hand):

Microsoft(R) Windows NT(TM)
(C) Copyright 1985-1996 Microsoft Corp.

C:\users>cd \temp

C:\TEMP>cd /users
The syntax of the command is incorrect.

C:\TEMP>cd \users

C:\users>

[interesting info snipped]
Thanks for clarification on VMS. It was *long* time when I've dealt with it.
Original problem was to extract
file name from the full path name, not to find last '/'.


If you really think that, you're reading impaired. The description of the
original problem is still included at the top of the article and it seems
to have everything to do with finding the last '/' character.


I *might* be reading impaired, but I still didn't get it why you my think so
based on OP and my assertions. Description of the problem seems to to have
everything to do with extracting file name from path name. Finding of last
'/'
is part of one of the solutions, but not a problem definition itself.

[snip]
Nov 14 '05 #33

P: n/a
In <bG7wc.1194$Oc1.654@newsfe4-gui> "Kieran Simkin" <ki****@digital-crocus.com> writes:
I needed a function to take a unix pathname like "/home/foo/dir/subdir/" and
create any necessary directories that didn't already exist in the path. For
example /home/foo may exist, but dir and dir/subdir might not. Here's the
code I came up with: (only non-standard functions here are mkdir() and
direxists() and they're not really part of the text processing algorithm).


There is a standard C function that provides the complete solution
(albeit not portably to non-Unix systems):

system("mkdir -p /home/foo/dir/subdir");

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #34

P: n/a
In <lU******************@twister01.bloor.is.net.cable .rogers.com> "nobody" <no****@nowhere.non> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9***********@sunnews.cern.ch...
In <NR***************@news04.bloor.is.net.cable.roger s.com> "nobody"

<no****@nowhere.non> writes:
>"Joe Laughlin" <Jo***************@boeing.com> wrote in message
>news:Hy********@news.boeing.com...
>> If I have a character array with "/some/file/directory/file_name", are
>there
>> any functions / libraries that I could use to separate the directory
>> ("some/file/directory") from the file name ("file_name").
>>

Even on command line, on modern Windows versions. It's only the good old
COMMAND.COM (R.I.P.) that had a problem with them.


Maybe that depends on definition of "modern". It doesn't work on NT 4.0
and I believe neither on 2K (don't have one on hand):

Microsoft(R) Windows NT(TM)
(C) Copyright 1985-1996 Microsoft Corp.

C:\users>cd \temp

C:\TEMP>cd /users
The syntax of the command is incorrect.

C:\TEMP>cd \users

C:\users>


That's because you're using it incorrectly, i.e. in contexts where the /
character has different semantics. Use it in contexts where there are no
such conflicts and it works:

T:\>h:/test
0000:0004

This command has executed the file H:\TEST.EXE.
>Original problem was to extract
>file name from the full path name, not to find last '/'.


If you really think that, you're reading impaired. The description of the
original problem is still included at the top of the article and it seems
to have everything to do with finding the last '/' character.


I *might* be reading impaired, but I still didn't get it why you my think so
based on OP and my assertions. Description of the problem seems to to have
everything to do with extracting file name from path name. Finding of last
'/'
is part of one of the solutions, but not a problem definition itself.


Feel free to provide a portable solution (i.e. a solution suitable for
this newsgroup) that is not based on finding of the last /.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #35

P: n/a
Hey u can always use strrchr function supplied with string.h
library...if u r using Microsoft C/Turbo C
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango-dot-charlie-oscar-mike.fake.org> wrote in message news:<10*************@corp.supernews.com>...
"Joe Laughlin" <Jo***************@boeing.com> wrote:
# Tom St Denis wrote:
# > Joe Laughlin wrote:
# >> If I have a character array with
# >> "/some/file/directory/file_name", are there any
# >> functions / libraries that I could use to separate the
# >> directory ("some/file/directory") from the file name
# >> ("file_name").

The problem as stated is not answerable without specifying the
operating system. NOS and TSOS didn't even have directories. On Mac System
7, that is a single file name.

# I'm not familar with C character array processing, sorry. Again, are there
# any standard libraries that would let me scan backwards, or do I need to do
# this manually?

If you have a unix available, try
manpage 3 string
and then follow the see-alsos.

You can also browse
http://www.squarebox.co.uk/cgi-squar.../man3/string.3
and follow the links.

Nov 14 '05 #36

P: n/a
kal
sh*************@yahoo.com (Shuvodeep) wrote in message news:<59**************************@posting.google. com>...
Hey u can always use strrchr function supplied with string.h
library...if u r using Microsoft C/Turbo C


1. My understanding is that "top posting" is bad manners here.

2. File name manipulation is related to file system and hence
is off topic. Here the discussion is about how to do it
in standard C assuming a certain file naming convention.

3. If you are using any of the C compilers you mentioned then
try looking up the "_makepath" and "_splitpath" functions.
Nov 14 '05 #37

P: n/a
kal wrote:
sh*************@yahoo.com (Shuvodeep) wrote in message
Hey u can always use strrchr function supplied with string.h
library...if u r using Microsoft C/Turbo C


1. My understanding is that "top posting" is bad manners here.


In addition silly obfuscative abbreviations such as "u r" are
considered childish and strongly discouraged. string.h is NOT a
library. It is normally a system header file, used to secure
proper declarations of various routines included in the standard
library.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #38

P: n/a
Joe Laughlin wrote:

If I have a character array with "/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.

If you are using Linux or Unix, there should be a function available
to separate out the file name from the path-filename combination.

--
+-------------------------------
| Charles and Francis Richmond
| richmond at plano dot net
| Re-Defeat Bush!!!
+-------------------------------
Nov 14 '05 #39

P: n/a
Joe Laughlin wrote:

If I have a character array with
"/some/file/directory/file_name", are there
any functions / libraries that I could use to separate the directory
("some/file/directory") from the file name ("file_name").

I looked at sscanf(), but that didn't seem to do what I wanted.


/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
puts(1 + strrchr("/some/file/directory/file_name", '/'));
return 0;
}

/* END new.c */

--
pete
Nov 14 '05 #40

This discussion thread is closed

Replies have been disabled for this discussion.