You are here:

C/How to extract the essential data

Advertisement


Question
Dear Sir,
I have encountered a problem when trying to extract the correct data from a .txt file...using visual c++ 6.0. I have use "FILE *infile" to read the file and used "fscanf" to search for "char", and used "isdigit" to try to differentiate integers to alpha.  But as I used fscanf to search for 'char', it wasn't able to recognize "690" as six hundred and ninety but instead returned 3 individual digits 6,9,0.
Below is the sample .txt file which i would read from.
"
Data Logged on September 16, 2006, at 15:09:27

Datalog_Params

Log_Mode = 2
Sampling_Factor = 8
Defined_Sample_Size = 4000
Captured_Sample_Size = 4000
Delay_Sample = 0
Static_Sample = 10


Smp_No.      AN_EnB_HYG   BY_Cmd_HDA   IJ_MJK_Value
0       -20001       -20000          -69
1       -20001       -20001          121
2       -20001       -20001          -90
.                 .              .                  .
.                 .              .                  .
.                 .              .                  .
.                 .              .                  .
3999        65536      1420544          305   
"
So what I need to do is to actually read the figures from some coloums (for e.g AN_EnB_HYG & IJ_MJK_Value)...from sample no.0 to 3999. And calculate the max, min and average for the figures under AN_EnB_HYG & IJ_MJK_Value.. Every information besides these 2 coloums of figures are redundant to me..Please advise...thanks!  

Answer
Hi Liwei

For this type of problems I would suggest you to take the table data's in a structure after parsing the entire table in the file

A Struct like this will satisfy your needs i believe:
typedef struct data
{
  int Smp_No;
  int AN_Enb_HYG;
  int BY_Cmd_HDA;
  int IJ_MKJ_Value;
}DATA;

It is recommended to use fread, fseek and ftell to read the data from the file and storing it in a array of structure created dynamically like

DATA *p = (DATA*)malloc(sizeof(DATA)* x);

where, x = no of entries in the table

And then I guess it would be much easier for you to perform a search or a sort on the array to find out the max, min and the average of a particular row or column whether its redundant or not.

For any more queries or doubt please feel free to inform me !

All the Best !!!

Regards
Joydeep Bhattacharya

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.