!!!Problem -Reading from a file

Hi everybody!I am a beginner in C++ and I can`t find a solution for this problem.I`ve written a program that calculates the points of sportmen in competition from 6 rounds,sums the points,sorts them, and outputs the first three sportmen with the highest points.I filled the 2 files with the data in the exe. file. The idea is to add a function,which reads from the file below and after the sort phase, the three sportmen to be outputed from the file,that i filled previously, after i pressed 3.Here is the code:

//&br is the number of the sportmen
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
#include<stdlib.h>
using namespace std;
const int N=20;
struct sportmen
{
string name;
double results[6];
double sum;
int place;
};
fstream fp;

void menu();
sportmen vhod_1();
void sort_1(sportmen mas[],int &br);
void top3_1(sportmen mas[]);


void main()
{ sportmen young[N];
int ans,br(0);

do
{menu();
do{cin>>ans;
} while((ans<1)||(ans>4));

switch(ans)
{
case 1:young[br++]=vhod_1();break;
case 2:sort_1(young,br);break;
case 3:top3_1(young);break;
case 4:cout<<"End of the program";exit(0);
}
}
while(ans!=4);
system("pause");
}

sportmen vhod_1()
{
sportmen y;

cout<<"Enter the name of the sportman"<<endl<<"\t";
cin>>y.name;

int i;

cout<<endl<<"Enter the points from the 6 rounds"<<"\t";
y.sum=0;
for(i=0;i<6;i++)
{
cout<<"Discipline number"<<i+1<<endl<<"\t";
cin>>y.results[i];
y.suma+=y.results[i];
}

fp.open("Results.dat",ios::binary|ios::app);
if(fp.fail())
{
cout<<"Error opening the file";exit(0);}
fp.write((char*)y.results,sizeof(double));

fp.close();
return y;
}

void sort_1(sportmen mas[],int &br)
{

int i,j,k;
string buf;
double buf2;
double buf3;

for(i=0;i<br;i++)
{
for(j=0;j<br-1;j++)
{
if(mas[j].sum<mas[j+1].sum)
{
buf2=mas[j].sum;
mas[j].sum=mas[j+1].sum;
mas[j+1].sum=buf2;

buf=mas[j].name;
mas[j].name=mas[j+1].name;
mas[j+1].name=buf;

for(k=0;k<6;k++)
{
buf3=mas[k].results[k];
mas[k].results[k]=mas[k+1].results[k+1];
mas[k+1].results[k+1]=buf3;
}
}
}}
fp.open("Info.dat",ios::binary|ios::out);

if(fp.fail())
{
cout<<"Error opening the file";exit(0);}

fp.write((char*)mas,br*sizeof(sportmen));

fp.close();
}

void top3_1(sportmen mas[])
{
int i;

for(i=0;i<3;i++)
{
cout<<endl<<mas[i].name<<"\t"<<mas[i].sum<<"\t";
}
}

void menu()
{
cout<<endl<<"Make your choice from the menu";

cout<<endl<<"*******************Menu********************";
cout<<endl<<"1.Enter the data from one sportman and record it in a file the results from the 6 disciplines";
cout<<endl<<"2.Sort the array of sportmen and record it in a file the whole information for everyone ";
cout<<endl<<"3.Output the 3 top sportmen with the highest points";
cout<<endl<<"4.Exit";
}
After the last fp.close() is the place ,where i want the data to be read and outputed after the sorting in the function.And one more thing- all,which is typed mas[],sportmen mas[], etc is an the array of the sportmen from the structure sportmen.
try:
 
ofstream fp; fp.open("Results.dat",std::ios::app);

instead of
fp.open("Results.dat",ios::binary|ios::app);
Last edited on
I tried it but unfortunately it didn`t work.I thought about typing fp.open("Info.dat",ios::binary|ios::in)
fp.read((char*)mas,br*sizeof(sportmen));
But it also didn`t work
Topic archived. No new replies allowed.