获取路径组件 [英] Getting path components

查看:42
本文介绍了获取路径组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个代码片段来分隔路径组件,即驱动器,

路径,文件名和扩展名?


非常感谢提前,


Aaron

Has anyone got a code snippet to separate out the path components, ie drive,
path, filename, and extension ?

Many thanks in advance,

Aaron

推荐答案

文章< 67 ********** ***@mid.individual.net>,

Aaron Gray< an ******** @ gmail.comwrote:
In article <67*************@mid.individual.net>,
Aaron Gray <an********@gmail.comwrote:

>有没有人有一个代码片段来分隔出路径组件,即驱动器,路径,文件名和扩展名?
>Has anyone got a code snippet to separate out the path components, ie drive,
path, filename, and extension ?



不,没有可移植的方法。


标准C不对文件名进行任何解释

传递给标准I / O库函数,并且不知道

什么是驱动器,路径或扩展?是。标准C还有

不知道用什么字符或字符来分隔

的各种组件,包括对

没有意见存在目录,更不用说目录分隔符。


此外,使用微软的NTFS文件系统,旧驱动器刻字

系统被取代,驱动器名称可以看起来像目录

名字; NTFS还支持虚拟挂载点,因此在目录Y中看起来像
a文件X实际上可能是带有

命令参数X的程序Y.这种使用备用数据Streams不是以任何语法方式区分的。


有一些有效的Unix文件名看起来完全相同

作为一些有效的MS Windows文件名,但还有完全不同的解释。然后是openVMS,MVS,以及其他奇怪的文件系统命名方案的b $ b。哦,是的,请记住

在MacOS中(在MacOS X之前)'':''是目录分隔符...

-

没有比为你的同胞服务更大的呼吁。

没有比帮助弱者更大的贡献。

没有更大的满足感而不是拥有做得好。

- Walter Reuther

No, there is no portable way to do that.

Standard C does not place any interpretation upon filenames
passed to the Standard I/O library functions, and has no idea
what a "drive", "path", or "extension" is. Standard C also has
no idea of what character or characters are used to seperate
the various components, including having no opinion on the very
existance of directories let alone the directory seperator.

Furthermore, with Microsoft''s NTFS filesystem, the old drive lettering
system is superceeded, and drive names there can look like directory
names; NTFS also supports virtual mount points, so what -looks- like
a file X in directory Y might in fact be program Y with
command argument X. This use of Alternate Data Streams is not
distinguished in any syntactic way.

There are valid Unix file names that look -exactly- the same
as some valid MS Windows file names, and yet have completely
different interpretation. Then there''s openVMS, and MVS, and lots
of other strange filesystem naming schemes. Oh yes and remember
that in MacOS (before MacOS X) that '':'' was the directory seperator...
--
"There is no greater calling than to serve your fellow men.
There is no greater contribution than to help the weak.
There is no greater satisfaction than to have done it well."
-- Walter Reuther


2008年4月21日16:34,Aaron Gray写道:
On 21 Apr 2008 at 16:34, Aaron Gray wrote:

有没有人有一个代码片段来分隔路径组件,即驱动器,

路径,文件名和扩展名?
Has anyone got a code snippet to separate out the path components, ie drive,
path, filename, and extension ?



使用POSIX,您可以组合使用dirname()和basename()。如果你需要它还有

还有realpath()。


我不知道MS-Windows上的等效功能是什么 - 也许

jacob navia将会回答。

With POSIX, you can use dirname() and basename() in combination. There''s
also realpath() if you need it.

I don''t know what the equivalent functions are on MS-Windows - maybe
jacob navia will drop in with the answer.


" Antoninus Twink" < no **** @ nospam.invalidwrote in message

news:sl ******************* @ nospam.invalid ...
"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...

2008年4月21日16:34,Aaron Gray写道:
On 21 Apr 2008 at 16:34, Aaron Gray wrote:

>有没有人得到一个代码片段来分隔路径组件,即
驱动器,路径,文件名和扩展名?
>Has anyone got a code snippet to separate out the path components, ie
drive,
path, filename, and extension ?



使用POSIX,您可以组合使用dirname()和basename()。如果你需要它还有

还有realpath()。


我不知道MS-Windows上的等效功能是什么 - 也许

jacob navia将会回答这个问题。来自BSD的


With POSIX, you can use dirname() and basename() in combination. There''s
also realpath() if you need it.

I don''t know what the equivalent functions are on MS-Windows - maybe
jacob navia will drop in with the answer.



basename.c: -

http://www.krugle.org/kse/files?query=basename#3


只是: -


strrchr(路径,'/'')

来自OpenSSH的
basename: -

http://www.krugle.org/kse/codespaces/DXRo7v来自OpenSSH的
dirname: -

http://www.krugle.org/kse/codespaces/B4cBVF

来自OpenSSH的
realpath: -

http://www.krugle.org/kse/codespaces / Cn10Ja


应该好又安全:)


谢谢,


Aaron

Aaron

basename.c from BSD :-

http://www.krugle.org/kse/files?query=basename#3

is just :-

strrchr(path, ''/'')

basename from OpenSSH :-

http://www.krugle.org/kse/codespaces/DXRo7v

dirname from OpenSSH :-

http://www.krugle.org/kse/codespaces/B4cBVF

realpath from OpenSSH :-

http://www.krugle.org/kse/codespaces/Cn10Ja

Should be nice and secure :)

Thanks,

Aaron
Aaron


这篇关于获取路径组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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