views:

95

answers:

3

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

+1  A: 

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.

Praveen S
+2  A: 

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.

Daniel Egeberg
scanf can do this kind of parsing already. `"%10s %10[^;];%f;%f;%f;%f;%f;%f;"` is a format string that matches the OPs description.
Luther Blissett
Thank you so much this really got me rolling
chriscol
A: 

Thank you all for your valued contributions.With your help I have being able to write a function which is not yet complete. Iwant to to be able to open the first file(source) which contains 30 files with extension of .msr . I want this function to open the source file, then for each file inside it, to extract the informations needed as I have explained earlier and for each file store the date which is uniform for each file and the mean value of column 3 and 6 in a single file.Thus the destination file will contain at each line three rows which are the date, mean(3) and mean(6) separated by space making it a total of 30 rows. Please, could you guide me on how to implement this.

include

include

include

int file_getline_analyse(char *infile,char *outfile,char *path,char *strline) {

    int return_value=0;

    FILE *fd=NULL;    // pointer for data source
    FILE *fo= NULL;   // Destination file
    char *file_path=NULL;     

    char *date, *tmp,*time;
    double sum, mean = 0;
    file_path=calloc((strlen(path)+strlen(infile)),sizeof(file_path));   
    if (file_path==NULL) {
        printf("file_path in get_line\n");
        exit(EXIT_FAILURE);
    }

    strcpy(file_path,path);    // copies the path entered in the function call to the allocated meomory 
    strcat(file_path,infile);  // concatenates the contents of the  allocated meomory from the source file

    fd=fopen(file_path,"r");

    fo = fopen(outfile, "w");

    if((fd==NULL) && (fo==NULL))  {
        return_value = -1;
    }
    else {
        int i=0;
        int j=0;
        while ((fgets (strline, BUFSIZ, fd))>0){
            date = strtok(strline, " ");
            time=strtok(NULL, " "); // skip over time
            tmp = strtok(NULL, ";");
            if (i == 3) { // get only the 3rd value
                sum += strtod(tmp, NULL);
                ++i;
                if(j== '\n') {
                    // Replacing the characters at the end of the line by 0:
                    char *p = strchr (strline, '\n');
                    if (p) {
                        *p = 0;
                    }
                    return_value = 0;
                    break;

                }
                j++;


            }

            mean = sum/(double)(j+1);

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

        }
        fclose (fd);
        fclose(fo);
    }

    free(file_path);
    file_path=NULL;

    return return_value;
}

enter code here

chriscol