Тема: Запис даних з файла в масив структур
В коді я вводжу дані і вони записуються в файл, допоможіть будь-ласка записати дані з файла в масив структур
#include <conio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct base //ñòðóêòóðà
{
string name;
int years;
string pos;
string club;
int goals;
int assists;
int matches;
float sallary;
}club;
void addData();
void clearData();
void saveData();
void showData();
int main()
{
int z;
do//ìåíþ âèêîíóºòüñÿ
{
cout<<"-----------------------------------------------------------------------------------\n";
cout<<"[1]Add player\n[2]Check players\n[3]Delete info\n[4]Exit\n";
cout<<"-----------------------------------------------------------------------------------\n";
cin >> z;
if(z==1)
{
int a;
do
{
addData();//---------------------çâåðåíåííÿ äî ôóíêö³¿ äëÿ ââîäó äàíèõ
saveData();// äëÿ çáåðåæííÿ äàíèõ
cout << "Do you want to add one more player ? Yes - press '1'; No - press'0'\n" << endl;
cin >> a;
}
while(a==1);
system("cls");
}
else if(z==2)//âèâåñòè ãðàâö³â ó ôîðì³ òàáëèö³ ç ôàéëó
{
showData();
}
else if(z==3)
{
clearData();
}
if(z==4)//âèõ³ä ç ïðîãðàìè
{
exit(1);
}
}
while(z!=4);
_getch();
return 0;
}
//-----------------------------------------------------------------------------------------------------------------
void addData()//ôóíêöÿ äëÿ ââîäó ³ çáåðåæåííÿ äàíèõ
{
int a;
cout<<"-----------------------------------------------------------------------------------\n";
base *b; // îáÿâëåííÿ âêàç³âíèêà íà ñòðóêòóðó
b=&club;
cout << "Enter name :"<< endl;
cin >>b->name;
cout << "Enter years :" << endl;
cin >>b->years;
cout << "Enter pos :" << endl;
cin >> b->pos;
cout << "Enter previous club :" << endl;
cin >> b->club;
cout << "Enter goals :" << endl;
cin >> b->goals;
cout << "Enter assists :" << endl;
cin >> b->assists;
cout << "Enter matches :" << endl;
cin >> b->matches;
cout << "Enter sallary(millions) :" << endl;
cin >> b->sallary;
cout<<"-----------------------------------------------------------------------------------\n";
}
//-----------------------------------------------------------------------------------------------------------------
void saveData()
{
base *b; // îáÿâëåííÿ âêàç³âíèêà íà ñòðóêòóðó
b=&club;
ofstream team ("RealMadrid.txt",ios::ate|ios::app);//çàïèñ ñòðóêòóðè â ôàéë
team<<b->name <<"\t"<< b->years << "\t" << b->pos <<"\t"<<b->club<< "\t" << b->goals << "\t"<< b->assists << "\t"<<b->matches << "\t"<< b->sallary << "\t"<< endl;
}
//-----------------------------------------------------------------------------------------------------------------
void clearData()
{
fstream clear_file("RealMadrid.txt", ios::out);
clear_file.close();
system("cls");
}
//-----------------------------------------------------------------------------------------------------------------
void showData()
{
system("cls");
cout<<"-----------------------------------------------------------------------------------\n";
cout<<"name\t"<<"years\t"<<"pos\t"<<"club\t"<<"goals\t"<<"assists\t"<<"matches\t"<<"sallary(millions)\n";
cout<<"-----------------------------------------------------------------------------------\n";
ifstream team("RealMadrid.txt");
while(team)
{
string str;
getline(team, str);
cout << str << endl;
};
}
//-----------------------------------------------------------------------------------------------------------------