寻找一个分割路径名的库 [英] Looking for a library to split pathnames

查看:55
本文介绍了寻找一个分割路径名的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有人知道一个库提供了分割

路径名的功能。它应该有点像下面的代码片段:


-----------------

char * path =" / home / user / Documents / Textdocuments / Bills";

char ** dirs;


splitPath(path,dirs);

------------------


`dirs''应包括:

dirs [0] ==" home"

dirs [1] ==" user"

....

dirs [4] ==" Bills"


有人知道这样的功能吗?

感谢您的帮助。


Sven-Thorsten Fahrbach


PS:如果存在更好的

等价物或它是'',它也可能是C ++函数更容易使用。我来自Perl背景和

我对C中的字符串操作不太熟悉。

解决方案



Sven-Thorsten Fahrbach写道:





有谁知道提供的图书馆分割

路径名的功能。它应该有点像下面的代码片段:



我怀疑有人会很快说这是关于主题的:-)

un * x系统上的dirname()和basename()函数将完成你需要的大部分




如果这样是一个家庭作业问题(它闻起来有点像),我会看看

以下问题: -


*你怎么锻炼路径名中有多少个元素?

*你如何分配空间来保存指针数组?

*你如何填充指针数组?


如果您打算使用C语言,您可以使用

字符串处理。这不是很难。


ma ********** @ pobox.com 写道:


Sven-Thorsten Fahrbach写道:


>嗨

有没有人知道一个提供分割路径名功能的库。它应该有点像下面的代码片段:



我怀疑有人会很快说这是关于主题:-)



糟糕,抱歉。我不是常规的usenet用户;)。我只是不知道在哪里

发布此内容。


>

dirname( )和un * x系统上的basename()函数将完成你需要的大部分




谢谢,我会看一下!


>

如果这是一个家庭作业问题(它有点像一个)[...]



不,它不是,老实说。 ;)我正在尝试编写一个小工具,需要

来完成这项工作。我已经使用过Perl,但我担心这可能太慢了。
$ blockquote class =post_quotes>
我会看看

以下问题: -


*你如何计算出路径名中有多少元素?

*你好吗?分配空间来保存指针数组?

*如何填充指针数组?


如果你打算用C工作,你可以像好好掌握

字符串处理。这并不困难。



我知道。我没有足够的练习。但即使它不是一个好的练习问题,它仍然是一个很好的练习!


感谢您的帮助!


Sven-Thorsten Fahrbach




Sven-Thorsten Fahrbach写道:

ma**********@pobox.com 写道:


Sven-Thorsten Fahrbach写道:





有谁知道图书馆提供分割

路径名的功能。它应该有点像下面的代码片段:



我怀疑有人会很快说这是关于主题:-)



糟糕,抱歉。我不是常规的usenet用户;)。我只是不知道

的其他地方发布这个。



如果它是关于便携式C标准的一部分,这是一个好地方

。如果它更具有平台特性,那么其他地方可能会更好($。$ b $美元)。


但是,如果我们接受它作为字符串处理和内存分配

练习,它可能是主题:-)



un * x系统上的dirname()和basename()函数将完成你需要的大部分




谢谢,我会看看!


如果这是一个家庭作业问题(它有点像一个)[...]



不,老实说,不是。 ;)我正在尝试编写一个小工具,需要

来完成这项工作。我已经使用过Perl,但我担心这可能太慢了。
$ blockquote class =post_quotes>
我会看看

以下问题: -


*你如何计算出路径名中有多少元素?

*你好吗?分配空间来保存指针数组?

*如何填充指针数组?


如果你打算用C工作,你可以像好好掌握

字符串处理。这并不困难。



我知道。我没有足够的练习。但即使它不是一个家庭作业问题,它仍然是一个很好的练习!



好​​的 - 我会通过字符串计算元素的数量

count" /"字符,并适当调整可能性

相对路径名(无前导/)和空基本名称(尾随

" /"),然后是malloc()或calloc()指针数组的空间。

取决于你是想使用路径计数还是使用null

指针来划分数组,你可能需要在数组中添加一个额外的插槽

(我非常喜欢使用null作为分隔符)。


你清楚了吗?你想如何处理相对路径名

和null基本名称?


然后你可以使用strtok() - 不是最好的函数,但它将

工作,通过路径名依次挑选个别元素




Hi

Does anybody know of a library that offers a function to split
pathnames. It should work somewhat like the following code snippet:

-----------------
char *path = "/home/user/Documents/Textdocuments/Bills";
char **dirs;

splitPath(path, dirs);
------------------

`dirs'' should then consist of:
dirs[0] == "home"
dirs[1] == "user"
....
dirs[4] == "Bills"

Does anybody know of a function like that?
Thanks for your help.

Sven-Thorsten Fahrbach

P.S.: It could also be a C++ function if there exists a better
equivalent or if it''s easier to use. I come from a Perl background and
I''m not that comfortable with string manipulation in C.

解决方案


Sven-Thorsten Fahrbach wrote:

Hi

Does anybody know of a library that offers a function to split
pathnames. It should work somewhat like the following code snippet:

I suspect someone will be along shortly to say this is Off-Topic :-)

The dirname() and basename() functions on un*x systems will do most of
what you need here.

If this is a homework question (it smells a bit like one), I''d look at
the following questions:-

* how do you work out how many elements are in the pathname?
* how do you allocate space to hold the array of pointers?
* how do you fill the array of pointers?

If you are going to work with C you may as well get to grips with
string handling. It''s not _that_ difficult.


ma**********@pobox.com wrote:

Sven-Thorsten Fahrbach wrote:

>Hi

Does anybody know of a library that offers a function to split
pathnames. It should work somewhat like the following code snippet:


I suspect someone will be along shortly to say this is Off-Topic :-)

Oops, sorry. I''m not a regular usenet user ;). I just didn''t know where
else to post this.

>
The dirname() and basename() functions on un*x systems will do most of
what you need here.

Thanks, I''ll have a look into that!

>
If this is a homework question (it smells a bit like one) [...]

No, it''s not, honestly. ;) I''m trying to write a small tool which needs
to do this job. I''d have used Perl but I fear it might be too slow for
my purposes.

I''d look at
the following questions:-

* how do you work out how many elements are in the pathname?
* how do you allocate space to hold the array of pointers?
* how do you fill the array of pointers?

If you are going to work with C you may as well get to grips with
string handling. It''s not _that_ difficult.

I know. I just haven''t got enough practise. But even though it''s not a
homework question it might still be a good exercise!

Thanks for your help!

Sven-Thorsten Fahrbach



Sven-Thorsten Fahrbach wrote:

ma**********@pobox.com wrote:

Sven-Thorsten Fahrbach wrote:

Hi

Does anybody know of a library that offers a function to split
pathnames. It should work somewhat like the following code snippet:

I suspect someone will be along shortly to say this is Off-Topic :-)

Oops, sorry. I''m not a regular usenet user ;). I just didn''t know where
else to post this.

If it''s about things which are part of the portable C standard, this is
a good place. If it''s more platform-specific, somewhere else may be
better (comp.unix.programmer?).

However, if we take it as a string handling and memory allocation
exercise, it''s probably on-topic :-)


The dirname() and basename() functions on un*x systems will do most of
what you need here.

Thanks, I''ll have a look into that!

If this is a homework question (it smells a bit like one) [...]

No, it''s not, honestly. ;) I''m trying to write a small tool which needs
to do this job. I''d have used Perl but I fear it might be too slow for
my purposes.

I''d look at
the following questions:-

* how do you work out how many elements are in the pathname?
* how do you allocate space to hold the array of pointers?
* how do you fill the array of pointers?

If you are going to work with C you may as well get to grips with
string handling. It''s not _that_ difficult.

I know. I just haven''t got enough practise. But even though it''s not a
homework question it might still be a good exercise!

OK - I''d count the number of elements by a pass through the string
counting "/" characters, and adjust appropriately for the possibility
of relative pathnames (no leading "/") and a null basename (a trailing
"/"), then malloc() or calloc() the space for the array of pointers.
Depending on whether you want to use a count of paths or have a null
pointer to delimit the array, you may need an extra slot in the array
(I quite like using a null as a delimiter).

Are you clear in your mind how you want to handle relative pathnames
and null basenames, by the way?

Then you could use strtok() - not the nicest function, but it would
work, to walk through the pathname picking out the individual elements
in turn.


这篇关于寻找一个分割路径名的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆