Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read specific data columns from a file in c

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 assistance to this to get going on this.

Regards

like image 464
chriscol Avatar asked Jul 28 '10 10:07

chriscol


People also ask

How do you read a specific column from a file in C?

Solution 1. Use fgets - C++ Reference[^] to read the text file line by line into the buffer. Then parse each line to get the column elements.

Which command is used to extract specific columns from the file?

2. To extract specific columns from a file, ____ command is used. Explanation: cut command is used for cutting specific columns.

How do you read the contents of a file into a buffer in C?

Steps To Read A File: Open a file using the function fopen() and store the reference of the file in a FILE pointer. Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread(). File close the file using the function fclose().


2 Answers

Here is a starting point:

#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.

like image 69
Daniel Egeberg Avatar answered Oct 16 '22 23:10

Daniel Egeberg


sscanf can be a start point. First you will have to read every line into a string and use sscanf to get the required parameters which are separated by a whitespace in that string.

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);

  return 0;
}

Output

Rudolph -> 12

There may be better and easier ways to do it which i am sure will be answered here at SO.

EDIT :

Ofcourse you can also parse each string using strtok or strtok_r(reentrant version of strtok). Check the examples in the link to get an idea.

like image 41
Praveen S Avatar answered Oct 17 '22 00:10

Praveen S