sscanf和sprintf

很少用到sscanf和sprintf,但是后来逐渐意识到丰富对编程语言的认识是十分重要的,循规守旧(总是用自己熟悉的)其实也是一种懒惰。

下面是一段摘抄:

sscanf()函数,一般在处理输入时,特别是输入一堆字符串的时候,旺旺使用空格隔开的,在空格隔开的时候,大家在读入的时候,可以先使用scanf("%s%s%s",str1,str2,str3)读入这3个串:若在str2中,有一种特殊的格式,例如时间20:30:26,如何简单的分解出来呢?就是用sscanf()函数读入到三个变量中,例如sscanf(str2,"%d:%d:%d",&hour,&minute,&second),这样是不是就简单很多了呢?同样,还有相应的sprintf()函数。这两个函数的前缀s可以理解为string,即与字符串相关的输入函数。

下面是官方手册上的解释(就不翻译了):

function

<cstdio>

sscanf

int sscanf ( constchar * s, const char * format, ...);

Read formatted data from string

Reads data from s and stores themaccording to parameter format into the locations given by theadditional arguments, as if scanf was used, but reading from s insteadof the standard input (stdin).

The additional arguments should point to already allocated objects of the typespecified by their corresponding format specifier within the format string.


Parameters

s

C string that the function processes as its source toretrieve the data.

format

C string that contains a format string that follows thesame specifications as format in scanf (see scanf fordetails).

... (additionalarguments)

Depending on the format string, thefunction may expect a sequence of additional arguments, each containing apointer to allocated storage where the interpretation of the extractedcharacters is stored with the appropriate type.
There should be at least as many of these arguments as the number of valuesstored by the format specifiers. Additional arguments are ignoredby the function.



Return Value

On success, the function returns the number of items inthe argument list successfully filled. This count can match the expected numberof items or be less (even zero) in the case of a matching failure.
In the case of an input failure before any data could be successfullyinterpreted, EOF is returned.


Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/* sscanf example */

#include <stdio.h>

 

int main ()

{

  char sentence []="Rudolph is 12 years old";

  char str [20];

  int i;

 

  sscanf (sentence,"%s %*s %d",str,&i);

  printf ("%s -> %d\n",str,i);

 

  return 0;

}



Output:

Rudolph -> 12

 

 

 

 

=====================================================================

 

function

<cstdio>

sprintf

int sprintf ( char *str, const char * format, ... );

Write formatted data to string

Composes a string with the same text that would beprinted if format was used on printf,but instead of being printed, the content is stored as a C string inthe buffer pointed by str.

The size of the buffer should be large enough to contain the entire resultingstring (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 asmany additional arguments as needed for format.


Parameters

str

Pointer to a buffer where the resulting C-string isstored.
The buffer should be large enough to contain the resulting string.

format

C string that contains a format string that follows thesame specifications as format in printf (see printf fordetails).

... (additionalarguments)

Depending on the format string, thefunction may expect a sequence of additional arguments, each containing a valueto 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 valuesspecified in the format specifiers. Additional arguments areignored by the function.



Return Value

On success, the total number of characters written isreturned. This count does not include the additional null-characterautomatically 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

 

 


你可能感兴趣的:(sscanf和sprintf)