I have a homework where i need to read from a file and save in arrays informations like the city name, the country code, the county name and the population
#include <stdio.h>
#include <string.h>
int main ()
{
struct Country
{
char name [100]; // Le nom ASCII
char code[100]; // Le code de deux lettres
};
//const struct Pays PAYS_BIDON = {"??", "??"};
struct City
{
char name[100]; // Le nom ASCII
long population; // La population
struct Country country; // Le pays de la ville
};
struct City city;
FILE* fp = fopen("fich.txt", "r");
if (!fp)
{
printf("Can't open file\n");
}
else
{
char buffer[1024];
while (fgets(buffer, 1024, fp))
{
int i =0;
char* value = strtok(buffer, "\t");
while (value)
{
strcpy(city.name,value);
value = strtok(NULL, "\t");
printf("name : %s\n", city.name);
}
}
fclose(fp);
}
return 0;
}
until here my code is working but when i try to read the country name it doesn't work :
#include <stdio.h>
#include <string.h>
int main ()
{
struct Country
{
char name [100]; // Le nom ASCII
char code[100]; // Le code de deux lettres
};
//const struct Pays PAYS_BIDON = {"??", "??"};
struct City
{
char name[100]; // Le nom ASCII
long population; // La population
struct Country country; // Le pays de la ville
};
struct City city;
FILE* fp = fopen("fich.txt", "r");
if (!fp)
{
printf("Can't open file\n");
}
else
{
char buffer[1024];
while (fgets(buffer, 1024, fp))
{
int i =0;
char* value = strtok(buffer, "\t");
while (value)
{
strcpy(city.name,value);
value = strtok(NULL, "\t");
printf("city name : %s\n", city.name);
strcpy(city.country.name,value);
value = strtok(NULL, "\t");
printf("country : %s\n", city.country.name);
}
}
fclose(fp);
}
return 0;
}
the data in the file are like this:
3040051 les Escaldes les Escaldes Ehskal'des-Ehndzhordani,Escaldes,Escaldes-Engordany,Les Escaldes,esukarudesu=engorudani jiao qu,lai sai si ka er de-en ge er da,Эскальдес-Энджордани,エスカルデス=エンゴルダニ教区,萊塞斯卡爾德-恩戈爾達,萊塞斯卡爾德-恩戈爾達 42.50729 1.53414 P PPLA AD 08 15853 1033 Europe/Andorra 2008-10-15
3041563 Andorra la Vella Andorra la Vella ALV,Ando-la-Vyey,Andora,Andora la Vela,Andora la Velja,Andora lja Vehl'ja,Andoro Malnova,Andorra,Andorra Tuan,Andorra a Vella,Andorra la Biella,Andorra la Vella,Andorra la Vielha,Andorra-a-Velha,Andorra-la-Vel'ja,Andorra-la-Vielye,Andorre-la-Vieille,Andò-la-Vyèy,Andòrra la Vièlha,an dao er cheng,andolalabeya,andwra la fyla,Ανδόρρα,Андора ла Веля,Андора ла Веља,Андора ля Вэлья,Андорра-ла-Велья,אנדורה לה וולה,أندورا لا فيلا,አንዶራ ላ ቬላ,アンドラ・ラ・ヴェリャ,安道爾城,안도라라베야 42.50779 1.52109 P PPLC AD 07 20430 1037 Europe/Andorra 2020-03-03
290594 Umm Al Quwain City Umm Al Quwain City Oumm al Qaiwain,Oumm al Qaïwaïn,Um al Kawain,Um al Quweim,Umm Al Quwain City,Umm al Qaiwain,Umm al Qawain,Umm al Qaywayn,Umm al-Quwain,Umm-ehl'-Kajvajn,Yumul al Quwain,am alqywyn,mdynt am alqywyn,Умм-эль-Кайвайн,أم القيوين,مدينة ام القيوين 25.56473 55.55517 P PPLA AE 07 62747 2 Asia/Dubai 2019-10-24
i need from the first line
city.name = les Escaldes ;
city.country.code = AD;
city.population = 15853;
i need from the second line
city.name = Andorra la Vella;
city.country.code = AE;
city.population = 20430;
i need from the third line
city.name = Umm Al Quwain City;
city.country.code = AD;
city.population = 62747;
so i don't know what the wrong thing i'm doing or the right thing to do