I would like to read a text file and store to strings some data that are important for me.I tried to do with strtok but wasnt successful?Can u help pls?
txtExample
@Greece
ASAS (FFF) - BLALAL BALLA BLALALAL - JAJAJAJAJ, XAKALXKAL
ZMAH (AVK) – Arvaikheer Airport – Arvaikheer, Ovorkhangai
ZMBH (BYN) – Bayankhongor Airport – Bayankhongor, Bayankhongor
@USA
TGYH (ASE) - BLALAL BALLA BLAL - JAJAJ, XAKKAL
ASFG (FRD) – Arvheer Aiort – Arvaier, Ovorkhangai
@France
NMJU (ART) - BLALAL BALL - JAJJAJ, XAKAL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
FILE* fp;
char data[3];
char fake[100];
char iata[3];
char icao[4];
char name[24];
char location[24];
char country[24];
char buffer[200];
char* pch;
int id=0;
fp=fopen("example.txt","r");
if(fp==NULL)perror("Error opening file");
else{
while (!feof(fp)) {
fgets(buffer,200,fp);
if(buffer[0]=='@'){
pch = strtok (buffer,"@");
strcpy(country,pch);
}
if (buffer[5]=='(' && buffer[9]==')')
{
pch = strtok (buffer," (),");
while (pch != NULL)
{
//printf ("%s\n",pch);
if(id==0){
strcpy(icao,pch);}
else if(id==1){
strcpy(iata,pch);}
else if(id==2){
strcpy(name,pch);}
else if(id==3){
strcat(name," ");
strcat(name,pch);}
else if(id==4){
strcpy(location,pch);
id=0;
printf("XORA= %s\n",country);
printf("IATA= %s\n",iata);
printf("ICAO =%s\n",icao);
printf("LOCATION =%s\n",location);
}
pch = strtok (NULL, " ()");
id++;
}
}
}
}
getchar();
}