Link to home
Start Free TrialLog in
Avatar of Tom3333
Tom3333

asked on

Tokenize in C

I used the following code to read a file but something is going wrong with the token number. Is anyone who know what is going wrong?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(){
   
    FILE *fp;
    char line[50];
    char *token,*token1,*token2,*token3;
    fp=fopen("Infofile.txt","r");
   
   
    if (fp ==NULL)
       {printf("File not exist\n");}
   
   while (!feof(fp))
   {
    fgets(line,60,fp);
    //printf("%s",line);
    ///////////////////////////////////
    token = strtok(line,"\n");
    token1 = strtok(token," ");
   
    //token2 = strtok(line," ");
    //  token3 = strtok(NULL," ");
    printf("%s\n",token1);
    getchar();
    ///////////////////////////////////
    }
    return 0;}



the format of the file is : name 1 10
Avatar of Infinity08
Infinity08
Flag of Belgium image

It would help if you explain what you see that makes you think something is going wrong ...
Avatar of Tom3333
Tom3333

ASKER

the problem is that present all the line each time instead of word by word.

For example if i have the line George 1 20 in the read file
This code showed all the line .

I would like to receive first the word George then the number 1 and then the number 20 before move to the second line.  



How to do this????
Avatar of Tom3333

ASKER

No response yet?
ASKER CERTIFIED SOLUTION
Avatar of AJRDev
AJRDev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial