Tom3333
asked on
read file in C
I created a code in C for read a file. The code is working properly except the the last line of the file.
The problem is that for the file :
A1 20 B1
B2 10 C1
my code is read :
A1
20
B1
B2
10
C1
B2
How to fix this???
The problem is that for the file :
A1 20 B1
B2 10 C1
my code is read :
A1
20
B1
B2
10
C1
B2
How to fix this???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX2 100
#define DELIMITER " \t,"
int main(void)
{
FILE *fp;
char data[MAX2];
char *data1;
char *data2;
char *data3;
fp=fopen("a.txt","r");
if (fp==NULL)
{
printf("Cannot open the file");
exit(1);
}
else
{
while(!feof(fp))
{
fgets(data,MAX2,fp);
data1=strtok(data,DELIMITE
//printf("HELLO:%s\n",data
while ((data2=strtok (NULL,DELIMITER))!= NULL)
{
//printf("DDD:%s\n",data2)
data3=strtok (NULL,DELIMITER);
//printf("SS:%s\n",data3);
}
}
}
fclose(fp);
return 0;
}