为什么strncpy(s,t,n)不会将'\0'放在最后? [英] Why strncpy(s, t, n) doesn't put '\0' at the end?

查看:340
本文介绍了为什么strncpy(s,t,n)不会将'\0'放在最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程初学者...

我想知道,为什么strncpy(s,t,n)不会在字符串的末尾加上''\0''。 br />
因为当我输出复制的字符串时,它输出的内容比我想要的多,

,直到我自己把''\ 0''放在最后。


但是,有时我不需要把''\ 0''它运作良好?



strncpy (s,t,n);

strcat(s,t1);

.....

正如我想要的那样工作。

I am a C programming beginner...
I wonder, why strncpy(s, t, n) does not put ''\0'' at the end of the string.
Because when I output the copied string, it output more than what I want,
until I put ''\0'' at the end by myself.

But, sometime I don''t need put ''\0'' and it work well??
Like
strncpy(s, t, n);
strcat(s, t1);
.....
work just what I want.

推荐答案

" * m?Z" <到**** @ world.com>在消息中写道

news:bu ********** @ news.seed.net.tw ...
"*m?Z" <to****@world.com> wrote in message
news:bu**********@news.seed.net.tw...
我是一名C编程初学者。 ..
我想知道,为什么strncpy(s,t,n)不会在字符串的末尾添加''\0''。
因为当我输出复制的字符串时,它会输出更多比我想要的更多,直到我自己把''\0''放在最后。

但是,有时候我不需要把''\ 0'''它运作良好??
strncpy(s,t,n);
strcat(s,t1);
....
工作正是什么我想要。
I am a C programming beginner...
I wonder, why strncpy(s, t, n) does not put ''\0'' at the end of the string.
Because when I output the copied string, it output more than what I want,
until I put ''\0'' at the end by myself.

But, sometime I don''t need put ''\0'' and it work well??
Like
strncpy(s, t, n);
strcat(s, t1);
....
work just what I want.




这取决于源字符串的长度。 strncpy()从t到s复制最多n个/ b $ b个字符。如果t小于n个字符,则s中剩余的

个字符用零填充。如果t是n个字符长或长,

前n个字符被复制,那就是它。所有这些都在我们手册的

中解释。你有没有先读过它?


彼得



It depends on how long the source string is. strncpy() copies up to n
characters from t into s. If t is shorter than n characters, remaining
characters in s are filled with zeros. If t is n characters long or longer,
the first n characters are copied and that''s it. All of this is explained in
our manual. Have you read it first?

Peter




" * m?Z" <到**** @ world.com>在消息中写道

news:bu ********** @ news.seed.net.tw ...

"*m?Z" <to****@world.com> wrote in message
news:bu**********@news.seed.net.tw...
我是一名C编程初学者。 ..
我想知道,为什么strncpy(s,t,n)不会在字符串的末尾添加''\0''。
因为当我输出复制的字符串时,它会输出更多比我想要的更多,直到我自己把''\0''放在最后。

但是,有时候我不需要把''\ 0'''它运作良好??
strncpy(s,t,n);
strcat(s,t1);
....
工作正是什么我想要。
I am a C programming beginner...
I wonder, why strncpy(s, t, n) does not put ''\0'' at the end of the string.
Because when I output the copied string, it output more than what I want,
until I put ''\0'' at the end by myself.

But, sometime I don''t need put ''\0'' and it work well??
Like
strncpy(s, t, n);
strcat(s, t1);
....
work just what I want.




这就是它的工作方式。如果count小于

源字符串的长度,则必须使用以下内容终止自己:


dest_str [count] =''\'0' ';


如果count大于源字符串的长度,则

目标字符串将填充为0,最多可计数。但你仍然可以使用
来执行dest_str [count] =''\'''(在这种情况下它只是多余的)。


strncpy( )确保你永远不会超过

字符串的长度(导致崩溃),并且如果源字符串

可能长于目标字符串(或者如果您不确定)。如果

源字符串更长,那么目标字符串将不会是

自动为空终止,所以你应该在最后放一个''\0''

如下:


strncpy(dest_str,source_str,MAX_LEN_OF_DEST_STR);

dest_str [MAX_LEN_OF_DEST_STR] =''\''' ;


如果源字符串比目标字符串短,则dest str

将已经以NULL结尾,第二行将是还原的(但是

什么!)。


注意_memccpy(dest,source,0,count)相当于

strncpy(dest ,来源,计数),但通常要快得多(至少在与Borland或Microsoft编译器编译的Windows平台上)。


希望这会有所帮助.. 。


肖恩



Thats just the way it works. If count is less than the length of the
source string then you must null terminate yourself with:

dest_str[count]=''\0'';

If count is greater than the length of the source string then the
destination string will be padded with 0s up to count. But you can still
do the dest_str[count]=''\0'' (it is just redundant in this case).

strncpy() is useful for ensuring that you never exceed the length of a
string (causing a crash), and you should always use it if the source string
could be longer than the destination string (or if you are not sure). If
the source string was longer then the destination string will not be
automatically null terminated so you should put a ''\0'' at the end yourself
as follows:

strncpy(dest_str,source_str,MAX_LEN_OF_DEST_STR);
dest_str[MAX_LEN_OF_DEST_STR]=''\0'';

If the source string was shorter that the destination string, the dest str
will already be NULL terminated and the second line will be redudant (but so
what!).

NOTE that _memccpy(dest,source,0,count) is equivalent to
strncpy(dest,source,count) but is generally much faster (at least on a
Windows platform compiling with Borland or Microsoft compilers).

Hope this helps...

Sean


" * m?Z" <到**** @ world.com>写道:
"*m?Z" <to****@world.com> wrote:
我是一个C编程初学者...
我想知道,为什么strncpy(s,t,n)不会把''\0''放在字符串的结尾。


历史原因。最初,strncpy()的目的是使用

特定类型的固定长度,空填充,不一定是null-

终止的char数组,而不是普通的C字符串好吧。

因为当我输出复制的字符串时,它输出的内容比我想要的多,
直到我自己把''\0''放在最后。

但是,有时候我不需要把''\ 0''它运行得很好??
I am a C programming beginner...
I wonder, why strncpy(s, t, n) does not put ''\0'' at the end of the string.
Historical reasons. Originally, strncpy() was intended to work with a
particular kind of fixed length, null-padded, not necessarily null-
terminated char array, not with ordinary C strings at all.
Because when I output the copied string, it output more than what I want,
until I put ''\0'' at the end by myself.

But, sometime I don''t need put ''\0'' and it work well??




strncpy()的作用是什么根据需要将尽可能多的源复制到目的地

,但如果源小于限制,则使用空字符填充其余的

。当然,这意味着结果将会发生

也会以空值终止;但是很可能它会将b
写成更多的空值。


使用strncat()通常会更好strncpy(),如果仅用于

效率;而不是


strncpy(dest,src,n);

dest [n] =''\''';


你可以使用


* dest =''\ 0'';

strncat(dest,src,n);


这将_also_导致最多n个字符,以null结尾,是

写入dest,但不会写入所有那些额外的null
如果src很短,则
个字符。


Richard



What strncpy() does is copy as much of the source into the destination
as required, but if the source is smaller than the limit, pad the rest
with null characters. That means, of course, that the result will happen
to be null-terminated as well; but it''s also quite likely that it will
have written more nulls than necessary.

It''s often better to use strncat() instead of strncpy(), if only for
efficiency; instead of

strncpy(dest, src, n);
dest[n]=''\0'';

you can use

*dest=''\0'';
strncat(dest, src, n);

which will _also_ result in at most n characters, null-terminated, being
written to dest, but will not have written all those extra null
characters if src is short.

Richard


这篇关于为什么strncpy(s,t,n)不会将'\0'放在最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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