<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[函数安全] 关于strncpy函数比strcpy更安全的学习。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Thu, 16 Jan 2014 02:05:25 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	C/C++中的strncpy()函数功能为将第source串的前n个字符拷贝到destination串，原型为：<br/><br/>char * strncpy ( char * destination, const char * source, size_t num );<br/>各个参数的含义显而易见，其中返回值与destination相同。<br/><br/><br/>这个函数会出现三种情况：<br/>1、num&lt;source串的长度（包含最后的&#039;&#92;0&#039;字符）：那么该函数将会拷贝source的前num个字符到destination串中（不会自动为destination串加上结尾的&#039;&#92;0&#039;字符）；<br/>2、num=source串的长度（包含最后的&#039;&#92;0&#039;字符）：那么该函数将会拷贝source的全部字符到destination串中（包括source串结尾的&#039;&#92;0&#039;字符）；<br/>3、num&gt;source串的长度（包含最后的&#039;&#92;0&#039;字符）：那么该函数将会拷贝source的全部字符到destination串中（包括source串结尾的&#039;&#92;0&#039;字符），并且在destination串的结尾继续加上&#039;&#92;0&#039;字符，直到拷贝的字符总个数等于num为止。<br/><br/>————————————————————————————————————<br/>三种情况，自己做个实践：<br/>情况一：<br/>代码：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@localhost strncpy]# cat strncpy.c 
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main(void)&#123;

&nbsp;&nbsp;char string[10];
&nbsp;&nbsp;char *str1=&quot;abcdefghi&quot;;
&nbsp;&nbsp;strncpy(string,str1,3);
&nbsp;&nbsp;string[3]=&#039;&#92;0&#039;; //1）（不会自动为destination串加上结尾的&#039;&#92;0&#039;字符）；
&nbsp;&nbsp;printf(&quot;%s&#92;n&quot;,string);
&nbsp;&nbsp;return 0;
&#125;
</textarea><br/>编译：<br/>[root@localhost strncpy]# make strncpy<br/>cc&nbsp;&nbsp;&nbsp;&nbsp; strncpy.c&nbsp;&nbsp; -o strncpy<br/>运行：<br/>[root@localhost strncpy]# ./strncpy <br/>abc<br/>————————————————————————————————————<br/>情况二：<br/>代码：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@localhost strncpy]# cat strncpy.c&nbsp;&nbsp;&nbsp;&nbsp;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main(void)&#123;

&nbsp;&nbsp;char string[10];
&nbsp;&nbsp;char *str1=&quot;abcdefghi&quot;;
&nbsp;&nbsp;strncpy(string,str1,10);
&nbsp;&nbsp;string[10]=&#039;&#92;0&#039;;//2）num=source串的长度,拷贝source的全部字符到destination串中（包含最后的&#039;&#92;0&#039;字符）
&nbsp;&nbsp;printf(&quot;%s&#92;n&quot;,string);
&nbsp;&nbsp;return 0;
&#125;
</textarea><br/>运行：<br/>[root@localhost strncpy]# ./strncpy <br/>abcdefghi<br/>————————————————————————————————————<br/>情况三：<br/>代码：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@localhost strncpy]# cat strncpy.c 
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main(void)&#123;

&nbsp;&nbsp;char string[10];
&nbsp;&nbsp;char *str1=&quot;abcd&quot;;
&nbsp;&nbsp;strncpy(string,str1,10);//3）num&gt;source串的长度,在destination串的结尾继续加上&#039;&#92;0&#039;字符情况.
&nbsp;&nbsp;printf(&quot;%s&#92;n&quot;,string);
&nbsp;&nbsp;return 0;
&#125;
</textarea><br/>运行：<br/>[root@localhost strncpy]# ./strncpy <br/>abcd<br/>参考：http://www.cnblogs.com/unimous/archive/2012/03/05/2381151.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [函数安全] 关于strncpy函数比strcpy更安全的学习。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>