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. :)
Store the data as the structure then convert the data into a string, not the other way around. It will make your life alot simpler.
But even the way you had it written, that will kinda confuse printf, which expects a char* (the format string) as the first argument. It should be more like this:
| CODE |
| printf("%i", transac.trackingId); |