home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_04 / 1n04072a < prev    next >
Text File  |  1990-07-29  |  975b  |  37 lines

  1.  
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include <string.h>
  5.  
  6. /* set up 3 "variable length records" with | field separator marks */
  7.  
  8. char *data_in[] =
  9. {
  10.  { "a|" "b|" "c|" "aaa"},    // record w/data in all fields
  11.  { "a|" "|"  "c|" "bbb"},    // record w/empty data field b
  12.  { "a|" "|"  "|" "ccc"}      // record w/empty b & c
  13. };
  14.  
  15. char a[4], b[4], c[4], d[5];          // home for the results
  16.  
  17. main()                        
  18. {
  19.     int count, i = 0;
  20.     char fmt[50];
  21.  
  22.     // load and display input format string
  23.     strcpy(fmt,     "%[^|]|%[^|]|%[^|]|%s");
  24.     printf("format string is: %s\n", fmt);
  25.     while( i < 3 )
  26.     {            // zero target fields
  27.         a[0] = b[0] = c[0] = d[0] = 0;
  28.                 // read the data into targets a-d
  29.         count = sscanf(data_in[i], fmt ,a, b, c, d);
  30.                 // view the (erroneous) input and result
  31.         printf("Data in  : %s\n", data_in[i]);
  32.         printf("Data read: a=%-5s  b=%-5s  ", a, b);
  33.         printf("c=%-5s  d=%-5s\n", c, d);
  34.         i++;
  35.     }
  36. }
  37.