Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313357
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 759
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-09 14:15
个人简介

〆 人生就是拼命地奔跑,和华丽的跌倒。 つ

文章分类

全部博文(69)

文章存档

2017年(2)

2016年(16)

2015年(21)

2014年(30)

分类: C/C++

2015-05-05 16:10:03

STRCPY(3)                  Linux Programmer’s Manual                 STRCPY(3)

NAME
       strcpy, strncpy - copy a string

SYNOPSIS
       #include

       char *strcpy(char *dest, const char *src);

       char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
       The  strcpy()  function  copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.The  strings  may  not overlap, and the destination string dest must belarge enough to receive the copy.
       //strcpy()函数复制字符串指向源代码,包括结束的空字节'\0',缓冲指向目的代码.这个字符串不会被覆盖,存放目的代码的空间必须足够大去接受复制过来的字符串.

       The strncpy() function is similar, except that at most n bytes  of  srcare  copied.  Warning: If there is no null byte among the first n bytesof src, the string placed in dest will not be null terminated.
       //strncpy()函数很小,在源代码中至少复制n个字节.警告:如果在源代码中复制的n个字节中没有空字节,那么这个字符串在目的代码中将不会以空字节结束.

       If the length of src is less than n, strncpy() pads  the  remainder  ofdest with null bytes.
      //如果源代码的的长度小于n,目的代码剩余的空间就会带有空字节.

       A simple implementation of strncpy() might be:
      //一个简单的strncpy()函数例子:

           char*
           strncpy(char *dest, const char *src, size_t n){
               size_t i;
               for (i = 0 ; i < n && src[i] != '\0' ; i++)
                   dest[i] = src[i];
               for ( ; i < n ; i++)
                   dest[i] = '\0';
               return dest;
           }

CONFORMING TO
       SVr4, 4.3BSD, C89, C99.

NOTES
       Some programmers consider strncpy() to be inefficient and error  prone.If  the  programmer knows (i.e., includes code
to test!) that the sizeof dest is greater than the length of src, then strcpy() can be used.
      //一些程序考虑到strncpy()函数是没有效率的和倾向性的。如果程序知道目的代码的大小等于源代码的长度,那么这个strcpy()函数
就会被使用。

       If there is no terminating null byte in the first n characters of  src,strncpy()  produces  an unterminated string in dest.
 Programmers often prevent this mistake by forcing termination as follows:
       //如果在源代码的n个自符中没有结束的空字节,那么strncpr()函数在目的代码中也不 会产生一个结束字符串。程序总是通过
强迫终止来阻止这个错误。如下:

           strncpy(buf, str, n);
           if (n > 0)
               buf[n - 1]= '\0';

BUGS
       If the destination string of a strcpy() is not large enough, then  any-thing  might  happen.   Overflowing
 fixed-length  string  buffers isa favorite cracker technique for taking complete control of the  machine.Any  time  a
 program  reads  or copies datainto a buffer, the programfirst needs to check that there’s enough space.  This may  be  
unneces-sary if you can show that overflow is impossible, but be careful: programscan get changed over time, in ways that may make
 the impossible possible.
       //如果strcpy()函数的结束字符串不是足够大,就会发生一些问题。固定长度 的字符串溢出缓冲区是一个完成控制机器好的一个
技能。有时候一个程序在缓冲区读或者复制数据,这个程序首先需要去检查空间是否足够大。溢出是可能的,但是要仔细:程序能
够及时的改变,用这种方式上使之成为可能。
   
SEE ALSO
       bcopy(3),  memccpy(3),  memcpy(3),  memmove(3),  strdup(3),  stpcpy(3),
       wcscpy(3), wcsncpy(3)

COLOPHON
       This page is part of release 3.22 of the Linux  man-pages  project. Adescription  of  the project, and information about
reporting
bugs, can be found at
      //这篇是linux3.2版本中手册项目的一部分。一个项目的描述,还有关于信息漏洞的报告,
能够在中找到。

GNU                               2009-06-01                         STRCPY(3)
(END) 

例:

点击(此处)折叠或打开

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

  3. char *mystrcpy(char *dest, char *src)
  4. {
  5.     int i=0;
  6.     while(1)
  7.     {
  8.         dest[i]=src[i];
  9.         if(src[i] == '\0')
  10.             break;
  11.         i++;
  12.     }
  13. }

  14. int main()
  15. {
  16.     char s1[13]="nihao shijie";
  17.     char s2[9]="hello hi";

  18.     //先打印s2的字符串
  19.     printf("s2 is: %s\n", s2);

  20.     //mystrcpy(s2,s1);
  21.     
  22.     //从s1中复制8个字符到s2中
  23.     strncpy(s2,s1,8);

  24.     //再打印复制后s2的字符串
  25.     printf("s2 is: %s\n", s2);

  26.     return 0;
  27. }

阅读(2326) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~