Current location: Hot Scripts Forums » Programming Languages » C/C++ » How can I read specific data columns from a file


How can I read specific data columns from a file

Reply
  #1 (permalink)  
Old 07-27-10, 08:09 AM
okwy okwy is offline
New Member
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question How can I read specific data columns from a file

Good day all,

I am a beginner in c programming.I have this problem and have have spent quite a huge amount of time on it without any considerable progress.

My problem is stated thus:

I have a series of files with the extension (.msr), they contain measured numerical values of more that ten parameters which ranges from date,time,temperature, pressure, .... that are separated by semi colon.
The examples of the data values are shown below.

2010-03-03 15:55:06; 8.01; 24.9; 14.52; 0.09; 84; 12.47;
2010-03-03 15:55:10; 31.81; 24.9; 14.51; 0.08; 82; 12.40;
2010-03-03 15:55:14; 45.19; 24.9; 14.52; 0.08; 86; 12.32;
2010-03-03 15:55:17; 63.09; 24.9; 14.51; 0.07; 84; 12.24;

Each of the files have as a name REG_2010-03-03,REG_2010-03-04,REG_2010-03-05,... and they are all contained in a single file.
1. I want to extract from each of the file the date information which in this case 2010-03-03, column 3 and column 6.

2.Find the statistical mean of the each of the columns of 3 and 6.

3. Then store the results in a new file which will only contain the date,and the calculated mean of the columns above for further analysis.

They are to be written in c.

I am really counting or your invaluable assitance to this to get going.

Regards
Reply With Quote
  #2 (permalink)  
Old 01-20-11, 03:05 AM
debrah debrah is offline
New Member
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a starting point:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char str[] = "2010-03-03 15:55:06; 8.01; 24.9; 14.52; 0.09; 84; 12.47;";
    char *date, *tmp;
    double mean = 0;
    int i;

    date = strtok(str, " ");
    strtok(NULL, " "); // skip over time
    while (tmp = strtok(NULL, ";")) {
        ++i;
        if (i == 3 || i == 6) { // get only the 3rd and 6th value
            mean += strtod(tmp, NULL);
        }
    }
    mean /= 2;

    printf("%s: %.2f\n", date, mean);

    return 0;
}
You'll just have to do something like that to each line.

Last edited by UnrealEd; 01-20-11 at 03:17 AM. Reason: added [code] tags
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form to Text File Justin171985 PHP 14 02-09-11 11:55 AM
Show data, read file, String... ecard104 Everything Java 1 10-29-07 03:15 PM
how to separate and read data from text file robbydweb PHP 3 12-17-04 02:22 AM
Upload file type and size limiter! Arctic ASP 1 08-02-03 07:06 PM


All times are GMT -5. The time now is 01:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.