sscanf(3C)
_________________________________________________________________
sscanf function
Perform a formatted read from an internal string.
_________________________________________________________________
Calling Sequence
#include <stdio.h>
char *string;
sscanf(string, format, arg [,arg ...]);
where string is a pointer to a character array.
Each arg is a pointer to a valid C variable or
constant.
format is a string containing three kinds of
objects:
* Whitespace characters: space, New Line, carriage
return, and tab, which causes sscanf to read input
up to the next non-whitespace character.
* Ordinary printable ASCII characters.
* Conversion specifications that start with %.
Description
The sscanf function takes the format string and matches the input
to it. It attempts to replace the format specifications with the
successive arguments, and read the result from the string into
the arguments.
Note that ordinary characters must match exactly. The sscanf
function skips multiple whitespace characters. For details of
conversion specification, see the scanf function.
Returns
The return value from sscanf is the number of items successfully
matched and assigned.
Related Functions
See also the fscanf, scanf, and sprintf functions.
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
sscanf(3C)
Example
/* Program test for the sscanf() function */
#include <stdio.h>
#define MAX 5
FILE *fp, *fopen();
char instring[25], name[MAX][12], course[MAX][8];
double grade[MAX];
int i = 0, j = 0, k = 0, l = 0;
main(argc, argv)
int argc;
char *argv[];
{
fp = fopen(argv[1], "r");
while (i < MAX) {
fgets(instring, sizeof(instring), fp);
if (3 != sscanf(instring, "%s %s %f", name[i],
course[i], &grade[i]))
break;
i++;
}
printf("\nNAMES\n\n");
while (j < MAX) {
printf("%s\n", name[j]);
j++;
}
printf("\nCOURSES\n\n");
while (k < MAX) {
printf("%s\n", course[k]);
k++;
}
printf("\nGRADES\n\n");
while (l < MAX) {
printf("%f\n", grade[l]);
l++;
}
}
A call to the program test with
x test classrec
where the file classrec contains
Brown Algebra 2.5
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
sscanf(3C)
Jones Biology 1.6
Roberts English 3.4
Smith Physics 3.2
Wilson Spanish 3.7
generates the output
NAMES
Brown
Jones
Roberts
Smith
Wilson
COURSES
Algebra
Biology
English
Physics
Spanish
GRADES
2.500000
1.600000
3.399999
3.199999
3.699999
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)