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.

 

發佈了15 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章