Extracting data from an array in C

This is a discussion on Extracting data from an array in C within the C Programming forums, part of the General Programming Boards category; I am writing a program in C which will extract data from an array. The array contains the data: char ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Question Extracting data from an array in C

    I am writing a program in C which will extract data from an array.

    The array contains the data: char test[] = "TRACKING ID: 1365 DATE: 20030901 TIME: 1200 ROUTE: 12 DRIVER: 005 STATUS: Nobody present";

    What I need to do is extract just the bit of the Tracking ID which says 1365 and store it in a typedef struct and then print the contents of the typedef struct.

    This is what I have done so far:


    Code:
    //last updated Monday 6 October 2003
    
    #include <stdio.h>
    
    int main()
    {    
    //typedef structure to hold Transaction data
    typedef struct
    {
        int trackingId;
        char transDate[9];
        char transTime[5];
        int route;
        int driver;
        char status[100];
    } Transaction;
    
    Transaction transac;
    
    // TEST CODE
    char test[] = "TRACKING ID: 1365 DATE: 20030901 TIME: 1200 ROUTE: 12 DRIVER: 005 STATUS: Nobody present";
    
    // EXTRACT THE TRACKING ID AS AN INTEGER and store it in the typedef struct
    
    transac.trackingId = strstr(test, "TRACKING ID"); //NULL if not found
    printf(transac.trackingId);
    
    
    
    }


    The problem with this is that it prints out the whole of the contents of the char test[] array, when I really want it to just print out the tracking ID which is 1365.

    Thanks for any help.



  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Try the following:

    Replace

    Code:
    transac.trackingId = strstr(test, "TRACKING ID"); //NULL if not found
    printf(transac.trackingId);
    with

    Code:
    char id[20] = {0} ;
    char* charFirstOccurence = strstr(test, "TRACKING ID"); 
    strncpy(id,charFirstOccurence,strlen("TRACKING ID") ;
    printf(id) ;
    I don't have a compiler with me...so if code above fails, feel free to curse me.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Thanks for your help.

    But when I ran the program, it printed out the words "TRACKING ID", when I need it to print the actual tracking ID itself, which in this case is the number 1365.

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Oh, I see. Silly me. In that case, its not so easy as I thought.

    You could parse it for tokens separated by spaces " ". And the 3rd token would contain your numeric ID.

    1st Token: TRACKING
    2nd Token: ID
    3rd Token: 1365

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You might also use sscanf to parse the string.
    Code:
    #include <stdio.h>
    
    typedef struct
    {
       int  trackingId;
       char transDate[9];
       char transTime[5];
       int  route;
       int  driver;
       char status[100];
    } Transaction;
    
    int main(void)
    {
       Transaction transac;
       const char test[] = "TRACKING ID: 1365 DATE: 20030901 "
                           "TIME: 1200 ROUTE: 12 DRIVER: 005 "
                           "STATUS: Nobody present";
       if ( sscanf(test, "TRACKING ID: %d DATE: %8s TIME: %4s "
                         "ROUTE: %d DRIVER: %d STATUS: %99[^\n]",
                   &transac.trackingId,
                    transac.transDate,
                    transac.transTime,
                   &transac.route,
                   &transac.driver,
                    transac.status) == 6 )
       {
          printf("TRACKING ID = %d\n",     transac.trackingId);
          printf("DATE        = \"%s\"\n", transac.transDate);
          printf("TIME        = \"%s\"\n", transac.transTime);
          printf("ROUTE       = %d\n",     transac.route);
          printf("DRIVER      = %d\n",     transac.driver);
          printf("STATUS      = \"%s\"\n", transac.status);
       }
       return 0;
    }
    
    /* my output
    TRACKING ID = 1365
    DATE        = "20030901"
    TIME        = "1200"
    ROUTE       = 12
    DRIVER      = 5
    STATUS      = "Nobody present"
    */
    [EDIT]Corrected my carelessness.[/EDIT]
    Last edited by Dave_Sinkula; 10-07-2003 at 06:05 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Thanks, it's working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21