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.
I want to extract from each of the file the date information which in this case 2010-03-03, column 3 and column 6.
Find the statistical mean of the each of the columns of 3 and 6.
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
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.
2. To extract specific columns from a file, ____ command is used. Explanation: cut command is used for cutting specific columns.
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().
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With