Reading a formatted data from a string using sscanf()/sscanf_s() C function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Reading a formatted data from a string using sscanf()/sscanf_s() C function

To show: How to use the sscanf()/sscanf_s() C function to read a formatted data from a string

 

// Using sscanf()/sscanf_s()/swscanf()

#include <stdio.h>

int main(void)

{

char s[] = "31298 87.375";

int x;

float y;

 

printf("Using sscanf()/sscanf_()\n");

printf("------------------------\n");

// sscanf(s, "%d%f", &x, &y); - using the secure version

sscanf_s(s, "%d%f", &x, &y);

printf("array, s[] = 31298 87.375\n");

printf("\n%s\n%s%6d\n%s%8.3f\n",

"The values stored in character array s are: ",

"Integer: ", x, "Float: ", y);

return 0;

}

 

Output example:

 

Using sscanf()/sscanf_s()

------------------------

array, s[] = 31298 87.375

The values stored in character array s are:

Integer: 31298

Float: 87.375

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index