home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* set up 3 "variable length records" with | field separator marks */
-
- char *data_in[] =
- {
- { "a|" "b|" "c|" "aaa"}, // record w/data in all fields
- { "a|" "|" "c|" "bbb"}, // record w/empty data field b
- { "a|" "|" "|" "ccc"} // record w/empty b & c
- };
-
- char a[4], b[4], c[4], d[5]; // home for the results
-
- main()
- {
- int count, i = 0;
- char fmt[50];
-
- // load and display input format string
- strcpy(fmt, "%[^|]|%[^|]|%[^|]|%s");
- printf("format string is: %s\n", fmt);
- while( i < 3 )
- { // zero target fields
- a[0] = b[0] = c[0] = d[0] = 0;
- // read the data into targets a-d
- count = sscanf(data_in[i], fmt ,a, b, c, d);
- // view the (erroneous) input and result
- printf("Data in : %s\n", data_in[i]);
- printf("Data read: a=%-5s b=%-5s ", a, b);
- printf("c=%-5s d=%-5s\n", c, d);
- i++;
- }
- }
-