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();
}

Recommended Answers

All 3 Replies

Line 49 does not have the same separators as line 28

It is no good just saying it wasn't successful, we do not know what it was meant to do or what it actually did unless you tell us.

I get bad results...like the strings contain non understandable characters.Is there an easier way to do what i want to do?
Its meant to read some info

->@Greece

Greece is the info i need

->ZMAH (AVK) – Arvaikheer Airport – Arvaikheer, Ovorkhangai

I need ZMAH, AVK, Avraikheer Airport, Avraikheer, Ovorkhangai in different strings.

Thanks in advance!!!

I node a similar question in one of the more recent posts. Did your question get solved there ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.