Indeed I did not write this code, and I'm trying to decipher and take what I need out of it.
I have a code with a number of pre-defined parameters, which are all included in the following header file(global.h):
Code:
#ifndef _GLOBAL
#define _GLOBAL
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
typedef struct parameters {
int P;
int L;
int N;
int Trials;
double Tfinal;
double D;
double J;
} parameters;
#endif
Along with the file I posted above, they are both called by the main code.
Code:
#include "global.h"
#include "io.h" /* the previous function */
int main(int argc, char *argv[])
{
parameters p;
fprintf(stderr, "Parsing command line.\n"); fflush(stderr);
if(parseCommandLineArguments2(&p, argc, argv)!=0) {exit(-1);}
fprintf(stderr,"Finished parsing command line.\n\n"); fflush(stderr); /*ignore this part, it is running just fine*/
fprintf(stderr, "Reading parameter file.\n"); fflush(stderr);
if(readParameterFile(&p)!=0) {exit(-1);}
fprintf(stderr, "Finished reading parameter file.\n\n"); fflush(stderr);
return 0;
}
Code:
#include "io.h"
int X;
int parseCommandLineArguments2(parameters *p, int argc, char *argv[])
{
p->P=100;
p->D=0.20;
p->L=500;
p->Tfinal = 300.0;
p->Trials = 1;
p->J=2.5;
p->N=50;
return 0;
}
int readParameterFile(parameters *p, int argc, const char *const *argv)
{
FILE *fp;
if((fp=fopen(argv[1], "r")) == NULL) {goto ERROR1;}
if(argc != 1)
{
printf("Error!\n");
return 0;
}
else
{
sscanf( argv[1], "%d", &X);
}
if (fp)
{
if(fscanf(fp,"P\t\t=\t%d\n", &p->P)!=1){goto ERROR2;}
fprintf(stderr, "P = %d\n", p->P);
if(fscanf(fp,"P\t\t=\t%lf\n", &p->D)!=1){goto ERROR2;}
fprintf(stderr, "D = %lf\n", p->D);
if(fscanf(fp,"P\t\t=\t%d\n", &p->L)!=1){goto ERROR2;}
fprintf(stderr, "L = %d\n", p->L);
if(fscanf(fp,"P\t\t=\t%lf\n", &p->Tfinal)!=1){goto ERROR2;}
fprintf(stderr, "Tfinal = %lf\n", p->Tfinal);
if(fscanf(fp,"P\t\t=\t%d\n", &p->Trials)!=1){goto ERROR2;}
fprintf(stderr, "Trials = %d\n", p->Trials);
if(fscanf(fp,"P\t\t=\t%lf\n", &p->J)!=1){goto ERROR2;}
fprintf(stderr, "J = %lf\n", p->J);
if(fscanf(fp,"P\t\t=\t%d\n", &p->N)!=1){goto ERROR2;}
fprintf(stderr, "N = %d\n", p->N);
fclose(fp);
ERROR1:
fprintf(stderr,"Failed to open parameter %s. Exiting.\n", argv[1]);
return -1;
ERROR2:
fprintf(stderr, "Error reading parameter file. Exiting.\n");
return -1;
}
else
{
perror(argv[1]);
}
return 0;
}
Essentially, I want to read the parameters in the struct and add ONE additional parameters which I will be changing in order to see the results of the output. The error is in the very first function I posted, how do I change that?
Edit: Should this fix the problem of entering one additional variable (X)?