-
change text/data in file
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
-
You may be having a problem with the end of line character that is contained in buff. The fgets() function includes this character in your string. You may want to test your buff variable to see if it contains this character ('\n') and if it is present remove it.
Jim
-
i would debug by printf'ing the value of findme and buff just before the if(strstr..).
-
10x for the ideas, but the problem is somewhere with strstr() statement.
I tried to add ptr = strstr( buff, findme ) and in the debugger I see " ptr 0x00000000 <Bad Ptr> char * " but if I use other function the same way ( ptr = strchr(buff, '\0') ) everything is fine.
I don't understand what's the problem with this specific function (I work with Visual Studio 2008/2010)...
-
strstr() tests if the second argument as a whole is in the first argument.
As Jim already told you, fgets() reads also the newline-character if there is enough room for it.
After line 22 "findme" will contain "Gbpom\n".
When you come across the line "Gbpom=1.000000" in your data file, the call to strstr() will look like
Code:
strstr("Gbpom=1.000000", "Gbpom\n")
and thus no match is found because there is no '\n' after the 'm' in the original string.
Bye, Andreas