从文件名中分离目录:代码审查! [英] Separating directory from file name: Code Review!

查看:65
本文介绍了从文件名中分离目录:代码审查!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前没有使用过很多C,所以我不知道这段代码是多么强大或好看。
是多少。我会感激任何人的反馈或批评!


谢谢,

Joe


#include< ; stdio.h>

#include< string.h>


#define BUF_LENGTH 1000

int main()

{

char * string =" / some / file / here / file_name" ;;


/ *文件应为/ file_name和file_name。递增后* /

char * file_name = strrchr(string,''/'');

file_name ++;


printf (文件名是%s \ n,file_name;


/ *初始化路径* /

char * path =(char *)malloc(BUF_LENGTH);

memset(路径,0,BUF_LENGTH);


/ *文件路径的长度应该是

*文件指针的位置和字符串指针。我不能总是

假设

*这个,对吧? * /

int length_of_file_path = file_name - string;


/ *将第一个length_of_file_path字符放入路径* /

strncat( path,string,length_of_file_path);


printf("文件路径是%s \ n",路径);


}

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>

#define BUF_LENGTH 1000

int main()
{
char *string = "/some/file/here/file_name";

/* File should be "/file_name", and "file_name" after incrementing */
char* file_name = strrchr( string, ''/'');
file_name++;

printf ("The file name is %s\n", file_name);

/* Initialize the path */
char *path = (char*)malloc(BUF_LENGTH);
memset(path, 0, BUF_LENGTH);

/* The length of the file path should be the difference between the
* location of file pointer and the string pointer. I can''t always
assume
* this, right? */
int length_of_file_path = file_name - string;

/* Put the first length_of_file_path characters into path */
strncat(path, string, length_of_file_path);

printf("The file path is %s\n", path);

}

推荐答案

2004年6月2日星期三00:48:45 GMT,Joe Laughlin

< Jo ***************@boeing.com>在comp.lang.c中写道:
On Wed, 2 Jun 2004 00:48:45 GMT, "Joe Laughlin"
<Jo***************@boeing.com> wrote in comp.lang.c:
我之前没有使用过很多C语言,所以我不知道这段代码是多么强大或好用。我会感激任何人的反馈或批评!

谢谢,
Joe


哎呀,我刚刚回答了你的原始问题,建议使用

strrchr()。

#include< stdio.h>
#include< string.h>


需要添加:


#include< stdlib.h>


.. ..在范围内有一个适当的malloc()原型。

#define BUF_LENGTH 1000


你知道< stdio.h>定义一个宏FILENAME_MAX?虽然我不知道今天常用的任何系统允许路径名称

超过1000个字符,如果你使用FILENAME_MAX,缓冲区将

总是足够大,可以在任何系统上编译任何合法的文件名。

编译。

int main()
{
char * string =" / some / file / here / file_name" ;;

/ *文件应为/ file_name和file_name。递增后* /
char * file_name = strrchr(string,''/'');


你需要在这里检查NULL。虽然strrchr()在这个例子中不能返回null

指针,但我认为在现实世界中你需要使用任意字符串来交替地输入
用户或从文件或命令行读取
。你的程序迟早会被递给一个不包含'/''字符的字符串。然后,当你
增加空指针或将其传递给printf()或strncat()时你会产生未定义的行为,最有可能导致你的操作系统

终止你的程序。

file_name ++;

printf("文件名是%s \ n",file_name);

/ *初始化路径* /
char * path =(char *)malloc(BUF_LENGTH);


永远不要在C中转换malloc()的返回值。如果这样做是为了关闭

up编译器警告,我们通过包含<对于malloc的原型,stdlib.h>

。如果没有这个原型,演员会消除

警告,但在某些平台上特别做错了。

memset(path,0,BUF_LENGTH);


首先,你不需要将整个缓冲区设置为0,因为你是b $ b。其次,如果你确实需要为一个

字符数组分配内存并将其全部归零,calloc()将在

一次调用中执行这两项操作。请注意,calloc()保证将

字符数组初始化为所有有效的''\ 0''值,但不能保证这样做

with任何其他类型。


但你真正需要的只是一个''\ 0''开头,所以你可以

只是代码:


* path =''\''';

/ *文件路径的长度应该是
*位置之间的差异文件指针和字符串指针。我不能总是假设这个,对吧? * /
int length_of_file_path = file_name - string;


最终''/'之前的字符串长度,不包括最终的

file_name - string。它不可能是任何其他东西(如果strrchr()确实

不返回NULL)。您可以随时假设这一点。

/ *将第一个length_of_file_path字符放入路径* /
strncat(path,string,length_of_file_path);


这个问题是length_of_file_path包含最后的''/'',

你不想出现在路径字符串中(或者你?)。如果不是
,请使用length_of_file_path - 1作为长度参数。

printf(文件路径为%s \ n,路径);

}
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
Oops, I just answered your original question suggesting the use of
strrchr().
#include <stdio.h>
#include <string.h>
Need to add:

#include <stdlib.h>

....here to have a proper prototype for malloc() in scope.
#define BUF_LENGTH 1000
Did you know that <stdio.h> defines a macro FILENAME_MAX? While I
don''t know of any system in common use today that allows a path name
to exceed 1000 characters, if you use FILENAME_MAX the buffer will
always be large enough for any legal file name on any system where it
is compiled.

int main()
{
char *string = "/some/file/here/file_name";

/* File should be "/file_name", and "file_name" after incrementing */
char* file_name = strrchr( string, ''/'');
You need to check for NULL here. While strrchr() can''t return a null
pointer in this example, I assume in the real world you will want to
work with arbitrary strings entered interactively by a user or read
from files or the command line. Sooner or later your program will be
handed a string that does not contain a ''/'' character. Then when you
increment the null pointer or pass it to printf() or strncat() you
generate undefined behavior, most likely causing your operating system
to terminate your program.
file_name++;

printf ("The file name is %s\n", file_name);

/* Initialize the path */
char *path = (char*)malloc(BUF_LENGTH);
Never cast the return value of malloc() in C. If you did this to shut
up compiler warnings, we fixed that properly by including <stdlib.h>
for malloc''s prototype. Without that prototype, the cast eliminates
the warning but specifically does the wrong thing on some platforms.
memset(path, 0, BUF_LENGTH);
First, you don''t need to set the entire buffer to 0 for what you are
doing. Second, if you do need to allocate memory for an array of
characters and have it all zeroed, calloc() will do both operations in
one call. Note that calloc() is guaranteed to initialize an array of
characters to all valid ''\0'' values, it is not so guaranteed to do so
with any other type.

But all you really need is one ''\0'' at the beginning, so you could
just code:

*path = ''\0'';
/* The length of the file path should be the difference between the
* location of file pointer and the string pointer. I can''t always
assume
* this, right? */
int length_of_file_path = file_name - string;
The length of the string prior to the final ''/'', excluding the final
''/'' but allowing for a ''\0'' in its place to terminate it, is exactly
file_name - string. It can''t ever be anything else (if strrchr() does
not return NULL). You can always assume this.
/* Put the first length_of_file_path characters into path */
strncat(path, string, length_of_file_path);
The problem with this is length_of_file_path includes the final ''/'',
which you do not want to appear in the path string (or do you?). If
you don''t, use length_of_file_path - 1 as the length argument.
printf("The file path is %s\n", path);

}




-

Jack Klein

主页: http://JK-Technology.Com

常见问题

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。 learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


文章< Hy ** ******@news.boeing.com> ;,

" Joe Laughlin" <乔*************** @ boeing.com>写道:
In article <Hy********@news.boeing.com>,
"Joe Laughlin" <Jo***************@boeing.com> wrote:
我之前没有使用过C,所以我不知道这段代码是多么强大或好看。我很感激任何人的反馈或批评!

谢谢,


#include< stdio.h>
#include< ; string.h>

#define BUF_LENGTH 1000
int main()
{
char * string =" / some / file / here / file_name" ;;

/ *文件应为/ file_name和file_name。递增后* /
char * file_name = strrchr(string,''/'');
file_name ++;


无法保证file_name中有''/''。会发生什么

然后呢?

printf(文件名是%s \ n,file_name;

/ *初始化路径* /
char * path =(char *)malloc(BUF_LENGTH);
memset(path,0,BUF_LENGTH);


无法保证文件名限制为1000个字符。

/ *文件路径的长度应该是
*文件指针的位置和字符串指针。我不能总是假设这个,对吧? * /
int length_of_file_path = file_name - string;

/ *将第一个length_of_file_path字符放入路径* /
strncat(path,string,length_of_file_path);

printf(文件路径是%s \ n,路径);

}
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>

#define BUF_LENGTH 1000

int main()
{
char *string = "/some/file/here/file_name";

/* File should be "/file_name", and "file_name" after incrementing */
char* file_name = strrchr( string, ''/'');
file_name++;
There is no guarantee that there is a ''/'' in file_name. What will happen
then?
printf ("The file name is %s\n", file_name);

/* Initialize the path */
char *path = (char*)malloc(BUF_LENGTH);
memset(path, 0, BUF_LENGTH);
There is no guarantee that the filename is limited to 1000 characters.
/* The length of the file path should be the difference between the
* location of file pointer and the string pointer. I can''t always
assume
* this, right? */
int length_of_file_path = file_name - string;

/* Put the first length_of_file_path characters into path */
strncat(path, string, length_of_file_path);

printf("The file path is %s\n", path);

}




严重错误我的Macintosh文件名如

" CB:Documents:Letters:Letter of 15 / May / 2004"。



Goes horribly wrong on my Macintosh with filenames like
"CB:Documents:Letters:Letter of 15/May/2004".


Jack Klein< ja *******@spamcop.net>写道:
Jack Klein <ja*******@spamcop.net> writes:
你知道< stdio.h>定义一个宏FILENAME_MAX?虽然我不知道今天常用的任何系统允许路径名称
超过1000个字符,但是b $ b

在Linux上,FILENAME_MAX是4096事实上,我不知道今天常用的任何Unix

系统是否/允许路径名至少为
1024个字符。

如果使用FILENAME_MAX,缓冲区总是足够大,可以在编译它的任何系统上使用任何
合法文件名。
Did you know that <stdio.h> defines a macro FILENAME_MAX? While I
don''t know of any system in common use today that allows a path name
to exceed 1000 characters,
On Linux, FILENAME_MAX is 4096. In fact, I don''t know of any Unix
system in common use today that does /not/ allow a path name of at least
1024 characters.
if you use FILENAME_MAX the buffer will always be large enough for any
legal file name on any system where it is compiled.




如果系统对最大文件名没有任何限制

大小怎么办?如果我按字面意思采用标准(并忽略非规范性的

脚注),则必须将FILENAME_MAX定义为此类系统上整个

地址空间的大小。


马丁

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \_ /



What if the system poses no arbitrary limit on the maximum file name
size? If I take the standard literally (and ignore the non-normative
footnote), `FILENAME_MAX'' must be defined to the size of the whole
address space on such systems.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


这篇关于从文件名中分离目录:代码审查!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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