Computing.Net > Forums > Programming > Extracting data from an array in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extracting data from an array in C

Reply to Message Icon

Name: Victoria123
Date: October 7, 2003 at 12:20:02 Pacific
OS: Windows XP Home
CPU/Ram: 2.4Ghz / 256mb
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: borelli35
Date: October 8, 2003 at 00:46:45 Pacific
Reply:

===================================================
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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Extracting data from an array in C

A little help in an array in C++! www.computing.net/answers/programming/a-little-help-in-an-array-in-c/11838.html

Working With Files in Binary in C++ www.computing.net/answers/programming/working-with-files-in-binary-in-c/4520.html

Intitializing an Array in C www.computing.net/answers/programming/intitializing-an-array-in-c/8192.html