Thread: Code Parsing in C/C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    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
    Last edited by sgies; 02-18-2008 at 10:37 AM. Reason: Misleading Title

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM