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,*to ken3;
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
FILE *fp;
char line[50];
char *token,*token1,*token2,*to
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
It would help if you explain what you see that makes you think something is going wrong ...
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????
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????
ASKER
No response yet?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.