473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Separating directory from file name

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
39 2654
In <Hy********@new s.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
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
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
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:Hy******** @news.boeing.co m...
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
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
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
"Mabden" <ma****@sbcglob al.net> writes:
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
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
Hey, Martin! I thought you killfiled me over some comedy I posted once! Did
you grow a sensual humor?

--
Mabden
Nov 14 '05 #17
Mabden wrote:
"CBFalconer " <cb********@yah oo.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
CBFalconer <cb********@yah oo.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
In <4n************ *******@newssvr 25.news.prodigy .com> "Mabden" <ma****@sbcglob al.net> writes:
"CBFalconer " <cb********@yah oo.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2334
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
6
1741
by: dave | last post by:
I really have 2 questions regarding the following xml snippet. The xml is a directory representation. <?xml version="1.0" standalone="yes"?> <FileSystem> <Row> <ID>1</ID> <Name>Root</Name> <Directory>Root</Directory> <Dir>true</Dir>
6
4255
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.