Witam,
Mam problem z posortowaniem pliku,niestety musze zrobic to bezposrednio na pliku i moim pomyslem bylo zastosowania sortowania babelkowe jednak nie dziala to tak jak powinno...
void Sort(std::string fileName,std::string key)
{
char* buffor = new char[sizeof(Person)];
char* buffor_2 = new char[sizeof(Person)];
bool change;
fstream fs(fileName.c_str(), ios::in|ios::out|ios::binary);
if(fs.is_open())
{
do
{
fs.seekp(0,ios::cur);
change = false;
while(true){
fs.read(buffor,sizeof(Person));
fs.read(buffor_2,sizeof(Person));
if(fs.eof())
break;
if(compare(*(Person*)buffor,*(Person*)buffor_2,key))
{
fs.seekp(-2*sizeof(Person),ios::cur);
fs.write(buffor_2,sizeof(Person));
fs.write(buffor,sizeof(Person));
change = true;
}
fs.seekp(-1*sizeof(Person),ios::cur);
}
}while(change);
}
else
{
std::cout<<"Nie mozna otworzyc pliku "<<fileName<<endl<<endl;
std::cout<<"Nacisnij dowolny przycisk by powrocic..."<<endl;
getchar();getchar();
}
fs.close();
}
bool compare(Person &a,Person &b,string key)
{
if(key == "-N>")
{
if(a.getName()>b.getName())
return true;
else
return false;
}
else if(key == "-N<")
{
if(a.getName()<b.getName())
return true;
else
return false;
}
else if(key == "-H>")
{
if(a.getHeight()>b.getHeight())
return true;
else
return false;
}
else if(key == "-H<")
{
if(a.getHeight()<b.getHeight())
return true;
else
return false;
}
else if(key == "-A>")
{
if(a.getAge()>b.getAge())
return true;
else
return false;
}
else if(key == "-A<")
{
if(a.getAge()<b.getAge())
return true;
else
return false;
}
}