sobota, 2 listopada 2024

Retrieving data from a file into C++ Builder components.



File: top_Population.txt (location in directory Project C++ Builder)

India 1450935791 17.78
Pakistan 251269164 3.08
China 1419321278 17.39
United_States 345426571 4.23
Nigeria 232679478 2.85
Indonesia 283487931 3.47





struct Date
{
char name_Country[64];
float population_Country;
float procent_World;
};

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 StringGrid1->Cells[0][0]="Country";
 StringGrid1->Cells[1][0]="Population";
 StringGrid1->Cells[2][0]="World Share";
 Button1->Caption="Open";
 Button2->Caption="Close";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 struct Date* population_Info=new struct Date[6];
 FILE* my_File=fopen("top_Population.txt","r");
 if(!my_File)
 {
ShowMessage("Can't open file!");
 }
 else
 {
  for(int i=0;i<6;i++)
  {
fscanf(my_File,"%s %f %f\n",population_Info[i].name_Country,
&population_Info[i].population_Country,&population_Info[i].procent_World);
  }
  fclose(my_File);
  for(int i=0;i<6;i++)
  {
StringGrid1->Cells[0][i+1]=UnicodeString(population_Info[i].name_Country);
StringGrid1->Cells[1][i+1]=FloatToStr(population_Info[i].population_Country);
StringGrid1->Cells[2][i+1]=FloatToStrF(population_Info[i].procent_World,ffGeneral,7,1)+"%";
  }

 }

 delete [] population_Info;
}


 

Brak komentarzy:

Prześlij komentarz