_stscanf_s 函数

MSDN 对这个函数描述。Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// crt_sscanf_s.c
// This program uses sscanf_s to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>
#include <stdlib.h>

<!-- more -->

int main( void )
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;

// Input various data from tokenstring:
// max 80 character string plus NULL terminator
sscanf_s( tokenstring, "%s", s, _countof(s) );
sscanf_s( tokenstring, "%c", &c, sizeof(char) );
sscanf_s( tokenstring, "%d", &i );
sscanf_s( tokenstring, "%f", &fp );

// Output the data read
printf_s( "String = %s\n", s );
printf_s( "Character = %c\n", c );
printf_s( "Integer: = %d\n", i );
printf_s( "Real: = %f\n", fp );
}

sscanf_s 取值的时候,需要在每个取值后面指定取值的最大大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>

int _tmain()
{
TCHAR str[] = _T("CSFS dfdfd");
TCHAR szBuff[100] = {0};
TCHAR szBuff2[100] = {0};
_stscanf_s(str, _T("%s%s"), szBuff, _countof(szBuff), szBuff2, _countof(szBuff2));
_tprintf(_T("%s\n%s\n"), szBuff, szBuff2);

return 0;
}