将目录与文件名分开 [英] Separating directory from file name
问题描述
如果我有一个带有/ some / file / directory / file_name的字符数组,那么
我可以用来分隔目录的任何函数/库
(" some / file / directory")来自文件名(" file_name")。
我看了sscanf(),但似乎没有做我想做的事。
谢谢,
Joe
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
推荐答案
Joe Laughlin写道:
Joe Laughlin wrote:
如果我有一个带有/ some / file / directory / file_name的字符数组,那么我可以用来分隔目录的任何函数/库吗? />(some / file / directory)来自文件名(" file_name")。
我看了sscanf(),但似乎并没有做我想要的。
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.
家庭作业问题BEGONE!
[暗示因为我无法解决你的工作失败
自己琐碎的问题....:在字符串中向后扫描第一个/]
Tom
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
Tom St Denis写道:
Tom St Denis wrote:
Joe Laughlin写道:
Joe Laughlin wrote:
如果我有一个带有
/ some / file / directory / file_name的字符数组,是否有任何
函数/我可以用来将
目录(some / file / directory)与文件名
(file_name)分开的库。
我看了sscanf (),但这似乎没有做我想要的。
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.
家庭作业问题BEGONE!
[暗示因为我想要你由于无法自己解决琐碎的问题而导致工作失败....:在第一个/
Tom
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
我不熟悉C字符数组处理,抱歉。再次,是否有任何标准库可以让我向后扫描,或者我需要手动执行
吗?
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?
>
" Joe Laughlin" <乔*************** @ boeing.com>在消息中写道
新闻:Hy ******** @ news.boeing.com ...
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
Tom St Denis写道:
Tom St Denis wrote:
Joe Laughlin写道:
Joe Laughlin wrote:
如果我有一个带有
/ some / file / directory / file_name的字符数组,那么我有什么功能/库吗?用于将
目录(some / file / directory)与文件名
(file_name)分开。
我查看了sscanf(),但是这似乎没有做我想要的。
家庭作业问题BEGONE!
[暗示因为我希望你因工作失败而失败
自己解决琐碎的问题....:第一次扫描
字符串/]
Tom
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
我对不熟悉C字符数组处理。再次,
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?
>
可能有一个库函数可以为你做这个,但它也很快就可以手动完成它。请参阅下面的示例:
void extractfilename(const char * path,char * filename){
int i,c = 0;
/ *将路径[n]中n的最高值放入i中(如果我们忽略\ 0)
* /
i = strlen(path) -1;
/ * itterate向后穿过数组减少我直到我们得到一个
''/''* /
while(i> 0){
if(path [i] ==''/'')
休息;
i - ;
}
/ * itterate从'/'后面的
之后的元素开始向前转换数组'并将剩余的字符转储到文件名* /
while(i< strlen(path)){
filename [c ++] = path [++ i];
}
}
我写这篇文章是为了更多地测试我自己的知识,邀请评论。
一个更好的函数版本会将目标大小的数据作为参数来避免缓冲区溢出但是我把它留给了
简单。代码使用-Wall -pedantic在gcc中编译没有警告。
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/
这篇关于将目录与文件名分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!