View Single Post
  #1 (permalink)  
Old February 18th, 2008, 12:36 PM
sgies sgies is offline
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Data Parsing in C/C++

Hi Guys,

I am looking for a solution of parsing data in C language, I have attached the code below with some comments in it. What actually I am looking for not to call the sscanf function with the number data is available (In this it is 3) as it is random and can be anywhere between 0 to 9.

Code:

main()
{
    char buffer[500] = "DATA: 3,43,56,32f402,44,57,32f403,45,58,32f404";
    char *buf = buffer;
    int count;
    sscanf(buffer, "DATA: %d", &count); //First Value is Count, get it in a variable.
    printf("Count - %d\n", count);
    buf+=8; //strlen("DATA: 3,") = 8
    for(int c = 0; c < count; c++)
    {
        int a1, a2, a3;
        sscanf(buf, "%d,%d,%x", &a1, &a2, &a3);
        printf("%d,%d,%x\n", a1, a2, a3);
        buf+=13;//strlen("43,56,32f402,");
    }
    return 0;
}

Can some one suggest any alternate solution with less number of calls to sscanf, in this sample code it is happenning count+1 times.

If anyone can point me to C++ library routine will be helpfull as well.

Many thanx,
Sgies
Reply With Quote