C/C++常用的字符串处理函数

1.strcpy strncpy

char *strcpy( char *strDestination,const char *strSource);

The strcpy function copies strSource, including the terminating null character, to the location specified bystrDestination. No overflow checking is performed when strings are copied or appended. The behavior ofstrcpy is undefined if the source and destination strings overlap

char *strncpy( char *strDest,const char *strSource,size_t count );

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length ofstrSource, a null character is not appended automatically to the copied string. Ifcount is greater than the length ofstrSource, the destination string is padded with null characters up to lengthcount. The behavior ofstrncpy is undefined if the source and destination strings overlap.

2.strcatstrncat

char *strcat( char *strDestination,const char *strSource);

The strcat function appends strSource to strDestination and terminates the resulting string with a null character. The initial character ofstrSource overwrites the terminating null character ofstrDestination. No overflow checking is performed when strings are copied or appended. The behavior ofstrcat is undefined if the source and destination strings overlap.

char *strncat( char *strDest,const char *strSource,size_t count );

The strncat function appends, at most, the first count characters ofstrSource tostrDest. The initial character of strSource overwrites the terminating null character ofstrDest. If a null character appears instrSource before count characters are appended,strncat appends all characters fromstrSource, up to the null character. Ifcount is greater than the length ofstrSource, the length of strSource is used in place of count. The resulting string is terminated with a null character. If copying takes place between strings that overlap, the behavior is uned.

3.strlen

size_t strlen( const char *string );

Each of these functions returns the number of characters in string, excluding the terminalNULL. No return value is reserved to indicate an error. 

4.sprintf_snprintf 

int sprintf( char *buffer,const char *format [,argument] ... );

sprintf returns the number of bytes stored in buffer, not counting the terminating null character.

The sprintf function formats and stores a series of characters and values inbuffer. Eachargument (if any) is converted and output according to the corresponding format specification informat. The format consists of ordinary characters and has the same form and function as theformat argument forprintf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

int _snprintf( char *buffer,size_tcount, const char *format [,argument] ... );

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) in buffer. Each argument (if any) is converted and output according to the corresponding format specification informat. The format consists of ordinary characters and has the same form and function as theformat argument forprintf. If copying occurs between strings that overlap, the behavior is undefined.

5.sscanf

int sscanf( const char *buffer, const char *format [,argument ] ...);

The sscanf function reads data from buffer into the location given by eachargument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier informat. Theformat argument controls the interpretation of the input fields and has the same form and function as theformat argument for thescanf function; see scanf for a complete description offormat. If copying takes place between strings that overlap, the behavior is undefined.

6.fscanf

int fscanf( FILE *stream,const char *format [,argument ]... );

Return Values

Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF forfscanf andfwscanf.

Remarks

The fscanf function reads data from the current position of stream into the locations given by argument (if any). Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. format controls the interpretation of the input fields and has the same form and function as the format argument forscanf; seescanf for a description of format. If copying takes place between strings that overlap, the behavior is undefined.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值