sprintf 与 strchr

发布时间:2017-1-23 0:25:47 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"sprintf 与 strchr",主要涉及到sprintf 与 strchr方面的内容,对于sprintf 与 strchr感兴趣的同学可以参考一下。

摘自 c++refercence     http://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf sprintf的使用方法: function <cstdio> sprintf int sprintf ( char * str, const char * format, ... ); Write formatted data to string Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str. The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version). A terminating null character is automatically appended after the content. After the format parameter, the function expects at least as many additional arguments as needed for format. Parameters str Pointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string. format C string that contains a format string that follows the same specifications as format in printf (see printf for details). ... (additional arguments) Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n). There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function. Return Value On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string. On failure, a negative number is returned. Example 1 2 3 4 5 6 7 8 9 10 11 /* sprintf example */ #include <stdio.h> int main () {   char buffer [50];   int n, a=5, b=3;   n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);   printf ("[%s] is a string %d chars long\n",buffer,n);   return 0; } Output: [5 plus 3 is 8] is a string 13 chars long sprintf 最常见的应用之一莫过于把整数打印到字符串中,所以,sprintf 在大多数场合可以替代 itoa。 如: //把整数123 打印成一个字符串保存在s 中。 sprintf(s, "%d", 123); //产生"123" 可以指定宽度,不足的左边补空格: sprintf(s, "%8d%8d", 123, 4567); //产生:" 123 4567" 当然也可以左对齐: sprintf(s, "%-8d%8d", 123, 4567); //产生:"123 4567" 也可以按照16 进制打印: sprintf(s, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐 sprintf(s, "%-8X", 4568); //大写16 进制,宽度占8 个位置,左对齐 strchr的用法: function <cstring> strchr const char * strchr ( const char * str, int character );       char * strchr (       char * str, int character ); Locate first occurrence of character in string Returns a pointer to the first occurrence of character in the C string str. The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string. Parameters str C string. character Character to be located. It is passed as its int promotion, but it is internally converted back to char for the comparison. Return Value A pointer to the first occurrence of character in str. If the character is not found, the function returns a null pointer. Portability In C, this function is only declared as: char * strchr ( const char *, int ); instead of the two overloaded versions provided in C++. Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* strchr example */ #include <stdio.h> #include <string.h> int main () {   char str[] = "This is a sample string";   char * pch;   printf ("Looking for the 's' character in \"%s\"...\n",str);   pch=strchr(str,'s');   while (pch!=NULL)   {     printf ("found at %d\n",pch-str+1);     pch=strchr(pch+1,'s');   }   return 0; } Output: Looking for the 's' character in "This is a sample string"... found at 4 found at 7 found at 11 found at 18  

下一篇:serveroutput & glogin.sql


关键词: sprintf 与 strchr