Hello, I have written a small C programm where I can read data in from a .csv file. This works fine.
But now I want to create a function to read the data in. So I want modify my programm.
I have tried to put the fopen() in a function and to return this value. But this not works.
Here is my original program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
FILE *fp;
fp = fopen("data.csv","r");
if (fp == NULL)
{
printf("Can not open file");
exit(0);
}
char line[100];
char *sp;
double acceleration;
double rotation;
double temperature;
double totalTemperature;
int count = 0;
double direction=0;
while(fgets(line, 100, fp )!= NULL)
{
sp = strtok(line, ",");
acceleration = atof(sp);
sp = strtok(NULL, ",");
rotation = atof(sp);
sp = strtok(NULL, ",");
temperature = atof(sp);
totalTemperature += temperature;
count++;
}
printf("The mean Temperature is: %.14f\n", totalTemperature/count);
fclose(fp);
return 0;
}
As I said I want to create a function named read_data(), i want to modify my orignal program. This function should read the data from the .csv file, but I did not know what the return should be look like.
I have tried something like that, but it did not worked:
Code:
char * read_data(char* filename)
{
FILE *fp;
fp = fopen("data.csv","r");
if (fp == NULL)
{
printf("Can not open file");
exit(0);
}
fclose(fp);
return fp;
}
Thanks for a help.