I have a parameters-data file in which I would like to change a parameter value. For some reason the program never reaches the statement " strcpy(newbuff, replacewith) " and just copies the file to a new one. There's some problem with " strstr( buff, findme ) " but I can't find it. The strings replacewith and findme are initialized correctly.
Any help will be much appreciated.
Code:
void changeIfile()
{
double inVal, finVal, delta;
double i;
FILE* fp;
char buff[MAX_LEN_SINGLE_LINE+2]; // the input line
char newbuff[MAX_LEN_SINGLE_LINE+2]; // the results of any editing
char findme[32];
char replacewith[32];
FILE *out;
fp=fopen("change_param.txt", "r");
if (fp==NULL)
{
printf("Cannot open file!\n");
exit(1);
}
// parameter input
fscanf(fp, "parameter:\n");
fgets( findme, 32, fp );
fscanf(fp, "initial value=%lf\n final value=%lf\n delta=%lf\n\n",&inVal, &finVal, &delta);
printf("initial value=%lf\n final value=%lf\n delta=%lf\n\n", inVal, finVal, delta);
fclose(fp);
for (i=inVal; i<=finVal; i+=delta)
{
// open data file
fp=fopen("POM_data_file.txt", "r");
if (fp==NULL)
{
printf("Cannot open file!\n\n");
exit(1);
}
// poen target file
out= fopen( "new.txt", "w" );
if (out==NULL)
{
printf("Cannot open file!\n");
exit(1);
}
sprintf(replacewith, "%s=%lf\n", findme, i);
while ( fgets( buff, 512, fp )!=NULL )
{
if ( strstr( buff, findme )!=NULL )
{
strcpy(newbuff, replacewith);
}
else
{
// the input line is the output line
strcpy( newbuff, buff );
}
fputs( newbuff, out );
}
fclose(fp);
fclose(out);
remove( "POM_data_file.txt" );
rename( "new.txt" , "POM_data_file.txt" );
go();
file_index++;
}
}
the data file:
Code:
BSinput=1.0
Gbvpm=1.0
Gbpom=1.000000
POM parameters
betaP=0.2
thetaP=0.1
SP synapse
tauP=5.0
Ssp0=0.0
Gsp=80.0
TAUFsp=750.0
Usp=0.05
usp0=0.05
xsp0=1.0
TAURsp=-1.0
parameter to change file:
Code:
parameter:
Gbpom
initial value=1.0
final value=1.0
delta=0.1