Hi pros and experts. I am experiencing some trouble in doing up a code which could evaluate the input into the different data types.
What i managed to acheived is to read a file and its content.
I used strtok to break the string into various 'tokens'.
Now the problem is how do i differentiate the tokens into the different data types. I tried using 'isdigit' and 'atoi' to no avail. help pls.
For example contents of 'myfile.txt' as follow:
12 43 + - = / -9ya -300
the program should print the output as
12 is a number.
43 is a number.
+ is a operator.
- is a operator.
/ is a operator.
-9ya is invalid.
-300 is a number.
The code i managed to acheived so far is as follows:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
FILE * pFile;
char string [100];
char *tokenPtr;
int i;
pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
fgets (string , 100 , pFile);
puts (string);
tokenPtr = strtok( string, " ");
while( tokenPtr != NULL ){
printf("%s\n",tokenPtr );
tokenPtr = strtok( NULL, " ");
}
fclose (pFile);
}
return 0;
}
Any help is deeply appreciated.