473,761 Members | 2,384 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 #1
39 2651
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
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
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:Hy******** @news.boeing.co m...
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
"Kieran Simkin" <ki****@digit al-crocus.com> wrote in message
news:Cuavc.582$ S83.270@newsfe4-gui...
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:Hy******** @news.boeing.co m...
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
"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
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
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
"Kieran Simkin" <ki****@digit al-crocus.com> writes:
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:Hy******** @news.boeing.co m...
> 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 *extractfilenam e (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_witho ut_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
In <10************ *@corp.supernew s.com> SM Ryan <wy*****@tang o-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

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
9336
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
10111
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
9948
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...
0
9765
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
6603
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
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.