Link to home
Start Free TrialLog in
Avatar of Tom3333
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???
Avatar of Tom3333
Tom3333

ASKER

I forgot to present my code. My code is:


#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,DELIMITER);
                  //printf("HELLO:%s\n",data1);
                        while ((data2=strtok (NULL,DELIMITER))!= NULL)
                              {
                              //printf("DDD:%s\n",data2);                                    
                              data3=strtok (NULL,DELIMITER);
                              //printf("SS:%s\n",data3);                                    
                              }

                  }
            }
      
fclose(fp);
return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America 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