Hi everyone,
I want to tokenise a text file with around 20 lines, and as the lines could be moved around, I want it to check the tokens for a certain word and if it contains that word, I want to equate the second token to it.
That's probably confusing so here's an example (condensed) text file:
Code:
Mass (kg) = 500
Wingspan (m) = 8.23
Wing area (m2) = 11.4
and the code I'm currently fighting with is
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
main ()
{
char *mass;
float m;
char *WINGSPAN;
float wingspan;
char *s;
float S;
char *param[40];
char *param2[40];
/* Open and print number of lines in file - all works fine */
/* Problem area */
/* tokenise to find actual parameters */
char delims[]="=";
for (i=1;i<=lineno+1; i++)
{
param[i]=strtok(arra[i],delims);
param2[i]=strtok(NULL,delims);
if (strcmp('Mass*',param[i])==0) mass=param2[i];
if (strcmp('Wingspan*',param[i])==0) WINGSPAN=param2[i];
if (strcmp('Wing area*',param[i])==0) s=param2[i];
}
m=atof(mass);
printf("Mass is %f kg\n",m);
wingspan=atof(WINGSPAN);
printf("Wingspan is %f m\n",wingspan);
S=atof(s);
printf("Wing area is %f m2\n",S);
}
I've gotten around it by using
Code:
for (i=1;i<=lineno+1; i++)
{
param[i]=strtok(arra[i],delims);
param2[i]=strtok(NULL,delims);
}
mass=param2[0];
m=atof(mass);
printf("Mass is %f kg\n",m);
WINGSPAN=param2[2];
wingspan=atof(WINGSPAN);
printf("Wingspan is %f m\n",wingspan);
s=param2[4];
S=atof(s);
printf("Wing area is %f m2\n",S);
but this isn't helpful when lines are added to the file or switched around!
Error messages are:
weightest.c:204:15: warning: character constant too long for its type
weightest.c: In function ‘main’:
weightest.c:204: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
weightest.c:204: error: incompatible types in assignment
weightest.c:205:15: warning: character constant too long for its type
weightest.c:205: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
weightest.c:206:15: warning: character constant too long for its type
weightest.c:206: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
weightest.c:211: warning: passing argument 1 of ‘atof’ from incompatible pointer type
There's obviously something wrong with how I've declared the pointers but I'm really not sure how to fix it so any advice would be gratefully received! Thank you.