Move the sources to trunk
[opencv] / apps / Hawk / CVEiCL / EiC / module / stdClib / test / testscanf.c
1 #include "stdio.h"
2
3 void testsscanf(void)
4 {
5     char * str = "10.5 15 hello";
6     int d;
7     char buf[50];
8     float fval;
9     sscanf(str,"%f %d %s",&fval,&d,buf);
10     printf("got -> %g %d %s\n",fval,d,buf); 
11 }
12
13 void testscanf(void)
14 {
15     float fval;
16     
17     long lval;
18     int i,k;
19     char buf[20];
20
21     printf("Input a char set:\n");
22     scanf("%[abcdefghijklmnopqrstuvwzyz] %*[^\n]",buf);
23     printf("got -> %s\n",buf);
24
25
26     printf("Input a float:\n");
27     scanf("%f",&fval);
28     printf("got -> %f\n",fval);
29
30     printf("Input int and string:\n");
31     scanf("%d %[abcdefghijklmno]", &i, buf);
32     printf("got -> %d [%s]\n",i, buf);
33
34     printf("Input long:\n");
35     scanf("%ld", &lval);
36     printf("got -> %ld\n",lval);
37
38     printf("Input  an int with more than 2 digits:\n");
39     scanf("%2d %d ", &i, &k);
40     printf("got -> %d  %d\n",i,k);
41
42 }
43
44
45
46 void testfscanf(void)
47 {
48     FILE *fp;
49     int count; float quant; char units[21], item[21];
50     
51     fp = fopen("/tmp/xxeicxx","w+");
52     fputs("2 quarts of oil\n"
53           "-12.8 degrees Celsius\n"
54           "lots of luck\n"
55           "10.0LBS       of\n"
56           "dirt\n"
57           "100ergs of energy",
58           fp);
59     fseek(fp,0L,SEEK_SET);
60
61     while(!feof(fp)) {
62         units[0] = item[0] ='\0';
63         count = fscanf(fp,"%f%20s of %20s",&quant, units, item);
64         fscanf(fp,"%*[^\n]");
65         fprintf(stdout,"count = [%d]  quant = [%g] units = [%s] item = [%s]\n",
66                count,quant,units,item);
67     }
68     fclose(fp);
69 }
70     
71
72 int main(void)
73 {
74     testfscanf();
75 /*    testscanf();*/
76     testsscanf();
77
78     return 0;
79     
80 }
81
82
83
84
85