Thread: Function not working how it should.

  1. #1
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34

    Function not working how it should.

    I have the following code, which tokenises a file of the form:

    Code:
    Air density (kg/m3) = 1.225
    Mass (kg) = 500
    Engine power (HP) = 100
    etc.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // FUNCTIONS
    
    int linecount(char filename[40])
      {
        FILE *countfile;
        int ch=0;
        int lineno=0;
        countfile=fopen(filename,"r");
        // Count number of lines and length of file      
        while ((ch = fgetc(countfile)) != EOF) if (ch == '\n') ++lineno;
        fclose(countfile);
        return (lineno);
      }
    
    // Checks if import file has been opened correctly.
    void importnullcheck(char filename[30])
      {
        FILE *fileopen;
        fileopen=fopen(filename,"r");
    
        if (fileopen==NULL) fprintf(stdout,"Could not open %s file to read.  Check the filename is correct.\n",filename);
          
        fclose(fileopen);
      }
    
    main()
      {
    
        char delims[]="=";
        char paramfilename[20],line[128];
        char *param;
        int paramlineno,i;
        FILE *parameters;
        float m,rho,P;
    
        fprintf(stdout,"\nType in the name and extension of the aircraft data file e.g. param.txt\n");
        scanf("%25s",paramfilename);
        importnullcheck(paramfilename);
        paramlineno=linecount(paramfilename);
    
        // Open parameters file
    
        parameters=fopen(paramfilename,"r");
        fseek(parameters, 0, SEEK_SET);
        i=0;
        while(fgets(line,sizeof line,parameters) !=NULL)
          {
    	param=strtok(line,delims);
    
    	if (strcmp("Mass (kg) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      m=atof(param);} 
           if (strcmp("Air density (kg/m3) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      rho=atof(param);}	    
    	if (strcmp("Engine power (HP) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      P=atof(param);}
    
           // etc. -about 30 different parameters.
    
          }
    
            fprintf(stdout,"Air density is %lf kg/m3\n",rho);
    	fprintf(stdout,"Mass is %lf kg\n",m);
    	fprintf(stdout,"Power is %lf HP\n",P);
            // etc.
    
      fclose(parameters);
      return(0);
          }
    The output when this is run is:
    Code:
    Type in the name and extension of the aircraft data file e.g. param.txt
    param.txt
    Air density is 1.225000 kg/m3
    Mass is 500.000000 kg
    Power is 100.000000 HP
    This is correct. What's the reason the above works, but putting it in a function:

    Code:
    // FUNCTION
    
    float ftokencomp(char string[12],char *comparison)
      {
        char delims[]="=";
        
        if (strcmp(string,comparison)==0) comparison=strtok(NULL,delims);
        return(atof(comparison));
      }
    Then putting
    Code:
    m=ftokencomp("Mass (kg) ",param); // in the tokenising 'while' loop
    fails? It compiles but the output is now
    Code:
    Type in the name and extension of the aircraft data file e.g. param.txt
    param.txt
    Air density is 1.225000 kg/m3
    Mass is 0.0000000 kg
    Power is 100.000000 HP
    Obviously something is wrong with my function, can anyone see the problem?

    Thanks in advance.
    Last edited by snoikey; 08-20-2010 at 07:33 AM. Reason: slight change to program.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    if (strcmp(string,comparison)==0) comparison=strtok(NULL,delims);
    Do you really want the above line to have an ";" at the end.
    I am not sure what it should be.

    Tim S.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34
    Thanks for the reply.

    Code:
    if (strcmp(string,comparison)==0) comparison=strtok(NULL,delims);
    is equivalent to

    Code:
    if (strcmp(string,comparison)==0) 
      {
        comparison=strtok(NULL,delims);
      }
    so that shouldn't be the problem (although you never know). I've just changed it to the second way of writing it to check and it's still not working. Thanks for looking though!

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Can you post the code using the function? I'm curious as to if you are still calling strtok() for the first time on this line.
    Code:
    param=strtok(line,delims);
    Also, atof() for me does not have any type that returns a float, only doubles. Maybe you're compiler/environment is different, but it does hurt to do
    Code:
    return (float)atof(param);
    Since your "Mass (kg) " line is first I suspect the string compare is not working like you want. atof() is probably returning zero because strncmp() is never returning 0, so the strtok() in the function is never getting executed.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34
    Thanks for the quick response. Yep, the code with the function implemented is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <complex.h>
    #include "performancefunctions.h"
    
    int linecount(char filename[40])
      {
        FILE *countfile;
        int ch=0;
        int lineno=0;
        countfile=fopen(filename,"r");
        // Count number of lines and length of file      
        while ((ch = fgetc(countfile)) != EOF) if (ch == '\n') ++lineno;
        return (lineno);
      }
    
    // Checks if import file has been opened correctly.
    void importnullcheck(char filename[30])
      {
        FILE *fileopen;
        fileopen=fopen(filename,"r");
    
        if (fileopen==NULL) fprintf(stdout,"Could not open %s file to read.  Check the filename is correct.\n",filename);
          
        close(fileopen);
      }
    
    main()
      {
    
        char delims[]="=";
        char paramfilename[20],line[128];
        char *param;
        int paramlineno,i;
        FILE *parameters;
        float m,rho,P;
    
        fprintf(stdout,"\nType in the name and extension of the aircraft data file e.g. param.txt\n");
        scanf("%25s",paramfilename);
        importnullcheck(paramfilename);
        paramlineno=linecount(paramfilename);
    
        // Open parameters file
    
        parameters=fopen(paramfilename,"r");
        fseek(parameters, 0, SEEK_SET);
        i=0;
        while(fgets(line,sizeof line,parameters) !=NULL)
          {
    	param=strtok(line,delims);
    
    	m=ftokencomp("Mass (kg) ",param);
    
    	/*if (strcmp("Mass (kg) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      m=atof(param);} */
           if (strcmp("Air density (kg/m3) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      rho=atof(param);}	    
    	if (strcmp("Engine power (HP) ",param)==0)
    	    { param=strtok(NULL,delims);
    	      P=atof(param);}
    
           // etc. -about 30 different parameters.
    
          }
    
            fprintf(stdout,"Air density is %lf kg/m3\n",rho);
    	fprintf(stdout,"Mass is %lf kg\n",m);
    	fprintf(stdout,"Power is %lf HP\n",P);
            // etc.
    
      fclose(parameters);
      return(0);
          }
    As you can see, I've only used the function on mass, which is why the other two are still working. The initial strtok appears to be working ok, for the other two parameters at least!

    I've changed the function to
    Code:
    float ftokencomp(char string[12],char *comparison)  // also tried definition as 'double'
      {
        char delims[]="=";
        float newfloat;  // also tried definition as 'double'
        
        if (strcmp(string,comparison)==0) comparison=strtok(NULL,delims);
        newfloat=atof(comparison);
        return(newfloat);
      }
    as what you said about returning the float made sense, still no luck though. I agree that it isn't, but any ideas why the strtok in the function isn't getting executed, when it runs fine when not in the function?

    Edit: Also, I'm using the GCC compiler in Linux.
    Last edited by snoikey; 08-20-2010 at 09:06 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In
    Code:
    float ftokencomp(char string[12],char *comparison)  // also tried definition as 'double'
      {
        char delims[]="=";
        float newfloat;  // also tried definition as 'double'
        
        if (strcmp(string,comparison)==0) comparison=strtok(NULL,delims);
        newfloat=atof(comparison);
        return(newfloat);
      }
    What is returned when your strcmp != 0?

    Jim

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a poor way to do this. At the least, it should have else if("next text is found"), to prevent all the re-evaluations of the line.

    A cleverer way to do this would be to make a 2D string array, and list all your words you're searching for:

    Code:
    char tokens[35][30] = {
    {"Airspeed"},
    {"Power"}, 
    ... 
    }
    Then use a simple while loop to find the next token (this assumes they are out of order in the text file). Even easier if they are not, to use a for loop.
    Code:
    i=0;
    while((strstr(line, tokens[i])) == NULL) {
      ++i;
    }
    
    /* you now have the line with the right token matched to it 
    
    You could then make a good switch statement out of the i integer. 
    A number of if statements will also work, of course, but aren't as clean.
    */

  8. #8
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34
    Thank you Adak, I'll do that instead.

    @Jim - probably irrelevant now, but good point, that's probably why it wasn't working.. it should have looped back round. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Calling function not working
    By plmokn in forum C Programming
    Replies: 13
    Last Post: 08-12-2010, 08:28 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM