c++ - Parsing string to get comma-separated integer character pairs -


i'm working on project i'm given file begins header in format: a1,b3,t11, 2,,5,\3,*4,344,00,. going sequence of single ascii character followed integer separated comma sequence ending 00,.

basically have go through , put each character/integer pair data type have takes both of these parameters , make vector of these. example, header gave above vector ('a',1), ('b',3),('t',11),(',',5)(' ',2),('\',3),('*',4),('3',44) elements.

i'm having trouble parsing it. far i've:

  • extracted header text file first character until before ',00,' header ends. can header string in string format or vector of characters (whichever easier parse)
  • tried using sscanf parse next character , next int adding vector before using substrings remove part of string i've analyzed (this messy , did not me right result)
  • tried going through string vector , checking each element see if integer, character, or comma , acting accordingly doesn't work multiple-digit integers or when character int

    i know can split string based on commas i'm not sure how , still split integers characters while retaining both , accounting integers need treat characters.

    any advice or useful standard library or string functions appreciated.

  • one possibility, of many, store data in structure. uses array of structures structure allocated needed malloc , realloc.
    parsing string can accomplished using pointers , strtol parse integer , give pointer character following integer. pointer can advanced use in next iteration ascii character , integer.

    #include <stdio.h> #include <string.h> #include <stdlib.h>  #define size 100  struct pair {     char ascii;     int  integer; };  int main( void) {     char input[] = "a1,b3,!0,t11, 2,,5,\\3,*4,34400,";     char *pt = input;//start pt pointing first character of input     char *end = input;     int each = 0;     int loop = 0;     int length = 0;     struct pair pairs[size] = { { '\0', 0}};      //assuming input end in 00, ( or ,00,)     //remove 3 ( or 4 ??) characters     length = strlen ( input);     if ( length > 3) {         input[length - 3] = '\0';     }     ( each = 0; each < size; each++) {         //get ascii character , advance 1 character         pairs[each].ascii = *pt;         pt++;         //get integer         pairs[each].integer = strtol ( pt, &end, 10);         //end==pt indicates expected integer missing         if ( end == pt) {             printf ( "expected integer\n");             break;         }         //at end of string?         if ( *end == '\0') {             //if there elements remaining, add 1 each 1 more used             if ( each < size - 1) {                 each++;             }             break;         }         //the character following integer should comma         if ( *end != ',') {             //if there elements remaining, add 1 each 1 more used             if ( each < size - 1) {                 each++;             }             printf ( "format problem\n");             break;         }         //for next iteration, advance pt 1 character past end         pt = end + 1;     }     //loop through , print used structures     ( loop = 0; loop < each; loop++) {         printf ( "ascii[%d] = %c   ", loop, pairs[loop].ascii);         printf ( "integer[%d] = %d\n", loop, pairs[loop].integer);     }      return 0; } 

    another option use dynamic allocation.
    uses sscanf parse input. %n capture number of characters processed scan. offset , add variables can used iterate through input. last scan capture ascii character , integer , return sscanf 2.

    #include <stdio.h> #include <string.h> #include <stdlib.h>  struct pair {     char ascii;     int  integer; };  int main( void) {     char input[] = "a1,b3,!0,t11, 2,,5,\\3,*4,34400,";     char comma = '\0';     char ascii = '\0';     int integer = 0;     int result = 0;     int loop = 0;     int length = 0;     int used = 0;     int add = 0;     int offset = 0;     struct pair *pairs = null;//so realloc work on first call     struct pair *temp = null;      //assuming input end in 00, ( or ,00,)     //remove 3 ( or 4 ??) characters     length = strlen ( input);     if ( length > 3) {         input[length - 3] = '\0';     }     while ( ( result = sscanf ( &input[offset], "%c%d%c%n"     , &ascii, &integer, &comma, &add)) >= 2) {//the last scan 2 items         if ( ( temp = realloc ( pairs, ( used + 1) * sizeof ( *pairs))) == null) {             fprintf ( stderr, "problem allocating\n");             break;         }         pairs = temp;          pairs[used].ascii = ascii;         pairs[used].integer = integer;         //one more element used         used++;         //the character following integer should comma         if ( result == 3 && comma != ',') {             printf ( "format problem\n");             break;         }         //for next iteration, add offset         offset += add;     }     ( loop = 0; loop < used; loop++) {         printf ( "ascii[%d] = %c   ", loop, pairs[loop].ascii);         printf ( "value[%d] = %d\n", loop, pairs[loop].integer);     }      free ( pairs);      return 0; } 

    Comments

    Popular posts from this blog

    php - How to display all orders for a single product showing the most recent first? Woocommerce -

    asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

    angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -