change text/data in file

This is a discussion on change text/data in file within the C Programming forums, part of the General Programming Boards category; I have a parameters-data file in which I would like to change a parameter value. For some reason the program ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    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

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    i would debug by printf'ing the value of findme and buff just before the if(strstr..).

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    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)...

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 06:54 PM
  2. Replies: 6
    Last Post: 03-07-2011, 02:15 PM
  3. How to Change Text File Font Size
    By space in forum Windows Programming
    Replies: 3
    Last Post: 08-08-2006, 12:42 AM
  4. Replies: 1
    Last Post: 10-30-2002, 05:45 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21