解析路径名称 [英] Parsing a path name

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

问题描述

我正在使用一个服务器,它将为我提供文件的路径名,

在众多路径中。所以从getenv我可能得到/ home / myweb / page1但是,

当然,会有很多变化。我不确定如何沿着这条路走下去的最佳方式。我应该一次读一个字符

还是使用scanf?例如,

/ home / mypage / page1 / page1 / page2 / page2等问题可能会出现问题。


我还没有编程几年,所以我可以使用一些提示,

提示,链接和C函数。 tia

I''m working with a server that will provide me the pathname to a file,
among many paths. So from getenv I may get /home/myweb/page1 but, of
course, there will be many variations of that. I''m unsure of the best
way to go about following the path. Should I read one char at a time
or use scanf? The problem could occur with something like
/home/mypage/page1/page1/page2/page2, for example.

I have not been programming in a few years so I could use a few hints,
tips, links and C functions. tia

推荐答案

drhowarddrfine说:
drhowarddrfine said:

我正在工作使用服务器将为我提供文件的路径名,

在众多路径中。所以从getenv我可能得到/ home / myweb / page1但是,

当然,会有很多变化。我不确定如何沿着这条路走下去的最佳方式。我应该一次读一个字符

还是使用scanf?例如,

/ home / mypage / page1 / page1 / page2 / page2这样的问题可能会发生。
I''m working with a server that will provide me the pathname to a file,
among many paths. So from getenv I may get /home/myweb/page1 but, of
course, there will be many variations of that. I''m unsure of the best
way to go about following the path. Should I read one char at a time
or use scanf? The problem could occur with something like
/home/mypage/page1/page1/page2/page2, for example.



大概是你定义了路径作为一系列短步骤,每个步骤开始
带有/字符的
,并且跟随另一个这样的步骤,或者通过字符串的

结尾 。


所以分步解析:


#include< stdio.h>


#define MAX_STEP_LEN 256 / *在你的例子中,8会完成! * /


int get_step(char * out,char ** cur,char sep,size_t max)

{

int rc = 0;

if(** cur!=''\''')

{

* out ++ = ** cur ;

++ * cur;

--max;


while(最大0&& ** cur! =''\ 0''&& ** cur!= sep)

{

* out ++ = ** cur;

++ * cur;

--max;

}

* out =''\ 0'';

rc = 1;

}

返回rc;

}


int main(无效)

{

char path [] =" / home / mypage / page1 / page1 / page2 / page2" ;;

char步[MAX_STEP_LEN] = {0};


char * curstep = path;

while(get_step(step,& curstep,''/' ',MAX_STEP_LEN))

{

printf(" [%s] \ n",step);

}


返回0;

}


输出:


[/ home ]

[/ mypage]
[/ page1]

[/ page1]

[/ page2]

[/ page2]


如果这样做你想要的,那很好。如果没有,它至少应该让你开始。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

Presumably you define "path" as "series of short steps, each of which begins
with a / character, and is followed either by another such step, or by the
end of the string".

So parse it in steps:

#include <stdio.h>

#define MAX_STEP_LEN 256 /* in your example, 8 would have done! */

int get_step(char *out, char **cur, char sep, size_t max)
{
int rc = 0;
if(**cur != ''\0'')
{
*out++ = **cur;
++*cur;
--max;

while(max 0 && **cur != ''\0'' && **cur != sep)
{
*out++ = **cur;
++*cur;
--max;
}
*out = ''\0'';
rc = 1;
}
return rc;
}

int main(void)
{
char path[] = "/home/mypage/page1/page1/page2/page2";
char step[MAX_STEP_LEN] = {0};

char *curstep = path;
while(get_step(step, &curstep, ''/'', MAX_STEP_LEN))
{
printf("[%s]\n", step);
}

return 0;
}

Output:

[/home]
[/mypage]
[/page1]
[/page1]
[/page2]
[/page2]

If that does what you want, great. If not, it should at least get you
started.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




" ; drhowarddrfine" < ro ******* @ gmail.com写信息

news:11 ******************** @ e3g2000cwe。 googlegroup s.com ...

"drhowarddrfine" <ro*******@gmail.comwrote in message
news:11********************@e3g2000cwe.googlegroup s.com...

我正在使用一个服务器,它将为我提供文件的路径名,

之间很多路。所以从getenv我可能得到/ home / myweb / page1但是,

当然,会有很多变化。我不确定如何沿着这条路走下去的最佳方式。我应该一次读一个字符

还是使用scanf?例如,

/ home / mypage / page1 / page1 / page2 / page2这样的问题可能会发生。
I''m working with a server that will provide me the pathname to a file,
among many paths. So from getenv I may get /home/myweb/page1 but, of
course, there will be many variations of that. I''m unsure of the best
way to go about following the path. Should I read one char at a time
or use scanf? The problem could occur with something like
/home/mypage/page1/page1/page2/page2, for example.



你在谈论什么*问题*

你实际上在尝试做什么?

What *problem* are you talking about?
What is it that you are actually trying to do?


>

我几年没编程了所以我可以使用一些提示,

提示,链接和C功能。 tia
>
I have not been programming in a few years so I could use a few hints,
tips, links and C functions. tia



-

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,软件重用项目

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


很难理解你想做什么 - 你可能想用

strtok来拆分你的字符串,用斜杠分隔
it is difficult to understand what you want to do - you might want to use
strtok to split up your string, delimited by slashes


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

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