Code:
#define STRLENGTH 1024
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Configuration
{
char filepath[1024]; //input file
//file reading parameters
long long int start;
long long int finish;
long long int readlength;
//filter parameters
double cutoff;
long int poles; //must be even
long int padding;
//detection parameters
double threshold;
double hysteresis;
long int numbins;
long int event_minpoints;
long int event_maxpoints;
long int subevent_minpoints;
};
typedef struct Configuration configuration;
void read_config(configuration *config)
{
char *configline;
char *name;
char *value;
char *newline;
long int cutoff = 0;
long int samplingfreq = 0;
if ((configline = malloc(STRLENGTH*sizeof(char)))==NULL)
{
printf("Cannot allocate config line string\n");
abort();
}
int test;
FILE *configfile;
if ((configfile = fopen("config.dat","r"))==NULL)
{
printf("Cannot find config file: \"config.dat\"!");
abort();
}
while ((configline = fgets(configline, STRLENGTH, configfile)) != NULL)
{
name = strtok(configline,"=");
value = strtok(NULL,"=\n");
if (strcmp(name,"readlength") == 0)
{
config->readlength = strtoll(value,NULL,10);
}
else if (strcmp(name,"start") == 0)
{
config->start = strtoll(value,NULL,10);
}
else if (strcmp(name,"finish") == 0)
{
config->finish = strtoll(value,NULL,10);
}
else if (strcmp(name,"cutoff") == 0)
{
cutoff = strtol(value,NULL,10);
}
else if (strcmp(name,"samplingfreq") == 0)
{
samplingfreq = strtol(value,NULL,10);
}
else if (strcmp(name,"poles") == 0)
{
config->poles = strtol(value,NULL,10);
}
else if (strcmp(name,"filterpadding") == 0)
{
config->padding = strtol(value,NULL,10);
}
else if (strcmp(name,"threshold") == 0)
{
config->threshold = strtod(value,NULL);
}
else if (strcmp(name,"hysteresis") == 0)
{
config->hysteresis = strtod(value,NULL);
}
else if (strcmp(name,"numbins") == 0)
{
config->numbins = strtol(value,NULL,10);
}
else if (strcmp(name,"event_minpoints") == 0)
{
config->event_minpoints = strtol(value,NULL,10);
}
else if (strcmp(name,"event_maxpoints") == 0)
{
config->event_maxpoints = strtol(value,NULL,10);
}
else if (strcmp(name,"subevent_minpoints") == 0)
{
config->subevent_minpoints = strtol(value,NULL,10);
}
}
config->cutoff = 2.0 *(double) cutoff/(double) samplingfreq;
free(configline);
fclose(configfile);
}
int main(int argc, char **argv)
{
configuration *config;
if ((config = (configuration *) malloc(sizeof(config)))==NULL)
{
printf("Cannot allocate config structure\n");
abort();
}
read_config(config);
printf("%lld\n%lld\n%lld\n%g\n%ld\n%ld\n",config->start,config->finish,config->readlength,config->cutoff,config->poles,config->padding);
system("pause");
return 0;
}
run that in the same directory as the config file I included in the first post, named "config.dat"