Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

===================================================
Well...the tracking id starts at character 14 and ends at character 17. If you know that this will be consistent then you can extract it with that information because char test[] simply defines an open array of type character named test. Like any other array, you reference its sub-structure (the individual characters in the string) by sub-scripting the array. As an example: to get character 14 out of the string you would assign it with the sub-script 14 like such:char ch;
ch=test[14];if you travers the character array in a loop (14 to 17) then you can extract each of the 4 digits (characters) you are looking for within a for loop.
borelli35

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |