Skip to content

Instantly share code, notes, and snippets.

@RawanLaz
Created May 1, 2015 20:29
Show Gist options
  • Save RawanLaz/8fa2c1f79646652c4213 to your computer and use it in GitHub Desktop.
Save RawanLaz/8fa2c1f79646652c4213 to your computer and use it in GitHub Desktop.
Student database using file structure
#include <iostream>
#include<fstream>
using namespace std;
#include<string.h>
#include<vector>
#include<algorithm>
//offset id
struct student
{
char ID[10];
char Name[50];
char Address[50];
char Mobile [11];
char GPA[5];
friend ostream& operator<<(ostream&in,student &s);
};
struct PIndex
{
char id[9];
short ofset;
};
vector<PIndex>primary;
bool primary_change=false;
void print_primary()
{
for(int i=0;i<primary.size();i++)
{
if(primary[i].ofset!=-1)
cout<<primary[i].id<<"\n";
}
}
struct SIndex_name
{
char name[50];
short inverted;
};
vector<SIndex_name>secondary_name;
bool secondary_name_change=false;
void print_secondary_name()
{
for(int i=0;i<secondary_name.size();i++)
{
if(secondary_name[i].inverted!=-1)
{
cout<<secondary_name[i].inverted<<"";
cout<<secondary_name[i].name<<"\n";
}
}
}
struct SIndex_GPA
{
char GPA[5];
short inverted;
};
vector<SIndex_GPA>secondary_GPA;
bool secondary_GPA_change=false;
short binary_search_secondary_name(char name[8]);
void print_secondary_GPA()
{
for(int i=0;i<secondary_GPA.size();i++)
{
if(secondary_GPA[i].inverted!=-1)
cout<<secondary_GPA[i].GPA<<"\n";
}
}
void sort_primary(int beg,int las);
short binary_search_secondary_GPA(char GPA[8]);
int compare(char arr[8],char arr2[8]);
void format(char name[50])
{
for(int i=strlen(name);i<50;i++)
{
name[i]='^';
}
name[49]='\0';
}
void rewrite_index_vector(fstream &index,fstream &file)
{
primary.clear();
index.clear();
short x=-1;
index.seekp(0);
index.write((char*)&x,sizeof(x));
primary_change=true;
short in;char ide[8];ide[8]='\0';
short size;
file.seekg(2,ios::beg);
while(true)
{
in=file.tellg();
file.read((char*)&size,sizeof(size));
if(file.eof())break;
PIndex p;
cout<<"size "<<size<<"\n";
char *record=new char[size];
file.read(record,size);
if(record[2]=='*')
{continue;}
short idsize;
memcpy (&idsize, &record[0], sizeof(idsize));//read size
for(int i=2;i<10;i++)//dont read the size
{
ide[i-2]=record[i];
}
p.ofset=in;
strcpy(p.id,ide);
cout <<" ID "<<ide<<"\n";
primary.push_back(p);
}
sort_primary(0,primary.size());
for(int i=0;i<primary.size();i++)
{
index.write((char*)&primary[i].ofset,sizeof(primary[i].ofset));
index.write(primary[i].id,8);
}
x=0;
index.seekp(0);
primary_change=false;
index.write((char*)&x,sizeof(x));
index.close();
}
/*void read_secondary_name_indexfromfile(fstream &sename)
{
secondary_name_change=true;
secondary_name.clear();
short x;
sename.seekg(0,ios::end);
int size=sename.tellg();
if(size==2)//empty
{
secondary_name_change=false;
return;
}
sename.seekg(0,ios::beg);
//arr=new PIndex[(size-2)/(10)];
int i=0;
sename.read((char*)&x,sizeof(x));//flag
short inv;
if(x==0)//the indexes are right
{
cout <<"okopko"<<(size-2)/(10);
while(true)
{
index.read(primary[i].id,8);
if(index.eof())break;
sename.read((char*)&in,sizeof(of));//offset
primary[i].ofset=of;
index.read(primary[i].id,8);//id
i++;
}
primary_change=false;//finish editing
}
else //need to rewrite indexes
{
//change in size;
}
}
*/
void readindexfromfile(fstream &index)
{
primary_change=true;
primary.clear();
short x;
index.seekg(0,ios::end);
int size=index.tellg();
if(size==2)//empty
{
primary_change=false;
return;
}
index.seekg(0,ios::beg);
//arr=new PIndex[(size-2)/(10)];
int i=0;
index.read((char*)&x,sizeof(x));//flag
short of;
if(x==0)//the indexes are right
{
cout <<"okopko"<<(size-2)/(10);
while(true)
{
index.read((char*)&of,sizeof(of));//offset
primary[i].ofset=of;
if(index.eof())break;
index.read(primary[i].id,8);//id
i++;
}
primary_change=false;//finish editing
}
else //need to rewrite indexes
{
//change in size;
}
}
student& cinstudent()
{
student s;
cout <<"ID ";
cin.getline(s.ID,9);
s.ID[strlen(s.ID)]='\0';
cout <<"Name ";
cin.getline(s.Name,51);
s.Name[strlen(s.Name)]='\0';
cout <<"Address ";
cin.getline(s.Address,51);
s.Address[strlen(s.Address)]='\0';
cout<<"Mobile ";
cin.getline(s.Mobile,12);
s.Mobile[strlen(s.Mobile)]='\0';
cout<<"GPA ";
cin.getline(s.GPA,5);
s.GPA[strlen(s.GPA)]='\0';
return s;
}
bool isequal(char arr[],char arr2[])
{
if(strlen(arr)!=strlen(arr2))return false;
for(int i=0;i<strlen(arr);i++)
{
if(arr[i]!=arr2[i])
return false;
}
return true;
}
int compare(char arr[],char arr2[])
//0 if equal, -1 if char1 is smaller , 1 if char1 is bigger
{
for(int i=0;i<strlen(arr)||i<strlen(arr2);i++)
{
if(arr[i]<arr2[i])
{
return -1;
}
else if(arr2[i]<arr[i])
{
return 1;
}
}
if(strlen(arr)>strlen(arr2))return 1;
else if(strlen(arr)<strlen(arr2)) return -1;
else return 0;
}
int compare_name(char name1[],char name2[])
{
int i;
for(i=0;i<strlen(name1)&&i<strlen(name2);i++)
{
if(name1[i]=='#'||name2[i]=='#')//reach the end of one of them and they are still the same
{
if(name1[i]=='#'&&name2[i]=='#')return 0;
else if (name1[i]!='#'&&name2[i]=='#')return 1;
else return -1;
}
if(name1[i]<name2[i])
{
return -1;
}
else if(name2[i]<name1[i])
{
return 1;
}
}
}
void sort_primary(int beg,int las)
{
for(int i=0;i<las;i++)
{
for(int y=i;y<las;y++)
{
if(compare(primary[i].id,primary[y].id)==1)
swap(primary[i],primary[y]);
}
}
}
void sort_secondary_name(int beg,int las)
{
for(int i=0;i<las;i++)
{
for(int y=i;y<las;y++)
{
if(compare(secondary_name[i].name,secondary_name[y].name)==1)
swap(secondary_name[i],secondary_name[y]);
}
}
}
void sort_secondary_GPA(int beg,int las)
{
for(int i=0;i<las;i++)
{
for(int y=i;y<las;y++)
{
if(compare(secondary_GPA[i].GPA,secondary_GPA[y].GPA)==1)
swap(secondary_GPA[i],secondary_GPA[y]);
}
}
}
void add_to_primary(PIndex p)
{
primary.push_back(p);
sort_primary(0,primary.size());
cout <<"\nprinting after adding to : ";print_primary();
}
void add_to_secondary_name(SIndex_name p)
{
format(p.name);
secondary_name.push_back(p);
sort_secondary_name(0,secondary_name.size());
//cout <<"printing after adding to : ";print_secondary_name();
}
void add_to_secondary_GPA(SIndex_GPA p)
{
secondary_GPA.push_back(p);
sort_secondary_GPA(0,secondary_GPA.size());
cout <<"\nprinting after adding to : \n";
print_secondary_GPA();
}
void rewrite_indexfromvector(fstream &index)
{
index.clear();
short x=-1;
index.seekp(0);
index.write((char*)&x,sizeof(x));
for(int i=0;i<primary.size();i++)
{
index.write((char*)&primary[i].ofset,sizeof(primary[i].ofset));
index.write(primary[i].id,8);
}
x=0;
index.seekp(0);
index.write((char*)&x,sizeof(x));
}
void rewrite_index_from_vector_name(fstream &sename)
{
sename.clear();
short x=-1;
sename.seekp(0);
sename.write((char*)&x,sizeof(x));
for(int i=0;i<secondary_name.size();i++)
{
sename.write(secondary_name[i].name,strlen(secondary_name[i].name));
sename.write((char*)&secondary_name[i].inverted,sizeof(secondary_name[i].inverted));
}
x=0;
sename.seekp(0);
sename.write((char*)&x,sizeof(x));
}
void rewrite_index_from_vector_GPA(fstream &seGPA)
{
seGPA.clear();
short x=-1;
seGPA.seekp(0);
seGPA.write((char*)&x,sizeof(x));
for(int i=0;i<secondary_GPA.size();i++)
{
seGPA.write(secondary_GPA[i].GPA,strlen(secondary_GPA[i].GPA));
seGPA.write((char*)&secondary_GPA[i].inverted,sizeof(secondary_GPA[i].inverted));
}
x=0;
seGPA.seekp(0);
seGPA.write((char*)&x,sizeof(x));
print_secondary_GPA();
}
void addstudent(fstream &file,fstream &index,fstream &sename,fstream &seGPA,fstream &invertlist_name,fstream &invertlist_GPA,student &s)// finish add
{
//check head
file.seekp(0,ios::end);
short size=0;int i=0;
short sizerec=strlen(s.ID)+strlen(s.Name)+strlen(s.Address)+strlen(s.Mobile)+strlen(s.GPA)
+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size);
char *record=new char[sizerec];
size=strlen(s.ID);
memcpy (&record[0], &size, sizeof(size));//2
i+=sizeof(sizerec);
int y;
y=0;int z;
for(z=i;z<strlen(s.ID)+i;z++)
{
record[z]=s.ID[y];
y++;
}
i+=size;
size=strlen(s.Name);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Name)+i;z++)
{
record[z]=s.Name[y];
y++;
}
i+=size;
size=strlen(s.Address);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Address)+i;z++)
{
record[z]=s.Address[y];
y++;
}
i+=size;
size=strlen(s.Mobile);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Mobile)+i;z++)
{
record[z]=s.Mobile[y];
y++;
}
i+=size;
size=strlen(s.GPA);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.GPA)+i;z++)
{
record[z]=s.GPA[y];
y++;
}
i+=size;
//before writing we have to check
short head;
file.seekg(0);
file.read((char*)&head,sizeof(head));
short offset;
//cout<<" add * "<<head<<" ";
if(head==-1)//nothing deleted before
{
file.seekp(0,ios::end);
offset=file.tellp();
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
}
else//fine
{
file.seekg(head,ios::beg);
file.read((char*)&size,sizeof(size));
//cout<<"size "<<size<<"\n";
if(sizerec==size)//works fine
{
char c;short next;short si;
file.read((char*)&si,sizeof(si));
//cout <<"the size of the id "<<si<<"\n";
file.get(c);
//cout <<"c "<<c<<" ";
file.read((char*)&next,sizeof(next));
//cout <<"NUM "<<next<<" ";
file.seekp(head,ios::beg);
offset=file.tellp();
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
short header=next;
file.seekp(0,ios::beg);
file.write((char*)&next,sizeof(next));
file.seekg(0);
file.read((char*)&header,sizeof(header));
//cout <<"the header after replacement "<<header<<"\n";
}
else if(sizerec>size||sizerec<size)//works fine
{
file.seekp(0,ios::end);
offset=file.tellp();
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
}
}
// write primary
index.seekg(0,ios::end);
int sizee=index.tellg();
index.seekg(0,ios::beg);short x;
index.read((char*)&x,sizeof(x));
if(sizee!=2)//we have indexes before
{
PIndex p;
strcpy(p.id,s.ID);
p.ofset=offset;
if(primary_change==false&&x==0)
{
add_to_primary(p);
rewrite_indexfromvector(index);
}
else
{
rewrite_index_vector(index,file);
}
}
else//first addition
{
index.seekp(0,ios::end);
index.write((char*)&offset,sizeof(offset));
index.write(s.ID,strlen(s.ID));
PIndex i;
strcpy(i.id,s.ID);
i.ofset=offset;
primary.push_back(i);
}//check later the change flag
//NAME
sename.seekg(0,ios::end);
sizee=sename.tellg();
sename.seekg(0,ios::beg);
//sename.read((char*)&x,sizeof(x));
format(s.Name);
cout<<"SO OS OS "<<s.Name;
if(sizee!=2)//we have namees before
{
if(binary_search_secondary_name(s.Name)==-1)
{//doesnt find the name
x=-1;
SIndex_name na;
strcpy(na.name,s.Name);
invertlist_name.seekp(0,ios::end);
na.inverted=invertlist_name.tellp();
invertlist_name.write(s.ID,strlen(s.ID));
invertlist_name.write((char*)&x,sizeof(x));
add_to_secondary_name(na);
sename.seekp(0);
sename.seekg(0);
rewrite_index_from_vector_name(sename);
//
invertlist_name.seekg(-2);
invertlist_name.read((char*)&x,sizeof(x));
cout<<" X WHEN ADDING "<<x;
}
else
{
short head_of_list;
short y=secondary_name[binary_search_secondary_name(s.Name)].inverted;
// invertlist_name.seekp(secondary_name[x].inverted,ios::end);
invertlist_name.seekp(0,ios::end);
short place_to_con=invertlist_name.tellp();//place in inverted list
invertlist_name.write(s.ID,strlen(s.ID));
x=-1;
invertlist_name.write((char*)&x,sizeof(x));
invertlist_name.seekp(y+8);
invertlist_name.seekg(y+8);
invertlist_name.read((char*)&x,sizeof(x));
if(y==-1)
{
invertlist_name.write((char*)&place_to_con,sizeof(place_to_con));
}
while(true&&y!=-1)
{
invertlist_name.seekg(y+8);
invertlist_name.seekp(y+8);
invertlist_name.read((char*)&y,sizeof(y));
if(invertlist_name.eof()){cout <<"there is wrong in the list\n";break;}
if(y==-1)
{
invertlist_name.seekp(-2,ios::cur);
//invertlist_name.write(s.ID,strlen(s.ID));
invertlist_name.write((char*)&place_to_con,sizeof(place_to_con));
}
}
}
}
else//first to add the name
{
secondary_name_change=true;
SIndex_name na;
strcpy(na.name,s.Name);//name
invertlist_name.seekp(0,ios::end);
na.inverted=invertlist_name.tellp();//inverted
invertlist_name.write(s.ID,strlen(s.ID));
x=-1;
invertlist_name.write((char*)&x,sizeof(x));
sename.seekp(0);
sename.seekg(0);
add_to_secondary_name(na);
rewrite_index_from_vector_name(sename);
secondary_name_change=false;
}
//Gpa
seGPA.seekg(0,ios::end);
sizee=seGPA.tellg();
seGPA.seekg(0,ios::beg);
//seGPA.read((char*)&x,sizeof(x));
//format(s.GPA);
cout<<"SO OS OS "<<s.GPA;
if(sizee!=2)//we have namees before
{
if(binary_search_secondary_GPA(s.GPA)==-1)
{//doesnt find the name
x=-1;
SIndex_GPA na;
strcpy(na.GPA,s.GPA);
invertlist_GPA.seekp(0,ios::end);
na.inverted=invertlist_GPA.tellp();
invertlist_GPA.write(s.ID,strlen(s.ID));
invertlist_GPA.write((char*)&x,sizeof(x));
add_to_secondary_GPA(na);
seGPA.seekp(0);
seGPA.seekg(0);
rewrite_index_from_vector_GPA(seGPA);
//
invertlist_GPA.seekg(-2);
invertlist_GPA.read((char*)&x,sizeof(x));
cout<<" X WHEN ADDING "<<x;
}
else
{
short head_of_list;
short y=secondary_GPA[binary_search_secondary_GPA(s.GPA)].inverted;
// invertlist_GPA.seekp(secondary_GPA[x].inverted,ios::end);
invertlist_GPA.seekp(0,ios::end);
short place_to_con=invertlist_GPA.tellp();//place in inverted list
invertlist_GPA.write(s.ID,strlen(s.ID));
x=-1;
invertlist_GPA.write((char*)&x,sizeof(x));
invertlist_GPA.seekp(y+8);
invertlist_GPA.seekg(y+8);
invertlist_GPA.read((char*)&x,sizeof(x));
if(y==-1)
{
invertlist_GPA.write((char*)&place_to_con,sizeof(place_to_con));
}
while(true&&y!=-1)
{
invertlist_GPA.seekg(y+8);
invertlist_GPA.seekp(y+8);
invertlist_GPA.read((char*)&y,sizeof(y));
if(invertlist_GPA.eof()){cout <<"there is wrong in the list\n";break;}
if(y==-1)
{
invertlist_GPA.seekp(-2,ios::cur);
//invertlist_GPA.write(s.ID,strlen(s.ID));
invertlist_GPA.write((char*)&place_to_con,sizeof(place_to_con));
}
}
}
}
else//first to add the name
{
secondary_GPA_change=true;
SIndex_GPA na;
strcpy(na.GPA,s.GPA);//name
invertlist_GPA.seekp(0,ios::end);
na.inverted=invertlist_GPA.tellp();//inverted
invertlist_GPA.write(s.ID,strlen(s.ID));
x=-1;
invertlist_GPA.write((char*)&x,sizeof(x));
seGPA.seekp(0);
seGPA.seekg(0);
add_to_secondary_GPA(na);
rewrite_index_from_vector_GPA(seGPA);
secondary_GPA_change=false;
}
//name,gpa;
seGPA.close();
invertlist_name.close();
invertlist_GPA.close();
sename.close();
index.close();
file.close();
}
short binary_search_primary(char ID[8])//return place in file
{
short RRN=-1;
short low = 0, mid, high = primary.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare(ID,primary[mid].id)==-1)
high = mid - 1;
else if (compare(ID,primary[mid].id)==1)
low = mid + 1;
else {
RRN=primary[mid].ofset;
break;
}
//else
}
return RRN;
}
short binary_search_secondary_name(char name[])//return place in file
{
short RRN=-1;
short low = 0, mid, high = secondary_name.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare_name(name,secondary_name[mid].name)==-1)
high = mid - 1;
else if (compare_name(name,secondary_name[mid].name)==1)
low = mid + 1;
else {
RRN=secondary_name[mid].inverted;
break;
}
}
return RRN;
}
short place_secondary_name(char name[ ])//return place in file
{
short RRN=-1;
short low = 0, mid, high = secondary_name.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare_name(name,secondary_name[mid].name)==-1)
high = mid - 1;
else if (compare_name(name,secondary_name[mid].name)==1)
low = mid + 1;
else {
RRN=mid;
break;
}
//else
}
return RRN;
}
short binary_search_secondary_GPA(char GPA[])//return place in file
{
short RRN=-1;
short low = 0, mid, high = secondary_GPA.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare(GPA,secondary_GPA[mid].GPA)==-1)
high = mid - 1;
else if (compare(GPA,secondary_GPA[mid].GPA)==1)
low = mid + 1;
else {
RRN=secondary_GPA[mid].inverted;
break;
}
//else
}
return RRN;
}
short place_secondary_GPA(char GPA[])//return place in file
{
short RRN=-1;
short low = 0, mid, high = secondary_GPA.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare(GPA,secondary_GPA[mid].GPA)==-1)
high = mid - 1;
else if (compare(GPA,secondary_GPA[mid].GPA)==1)
low = mid + 1;
else {
RRN=mid;
break;
}
//else
}
return RRN;
}
short place_in_vector(char ID[8])
{
short RRN=-1;
short low = 0, mid, high = primary.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (compare(ID,primary[mid].id)==-1)
high = mid - 1;
else if (compare(ID,primary[mid].id)==1)
low = mid + 1;
else{
RRN=mid;
break;
}
}
return RRN;
}
void delete_student_by_ID (fstream &file, fstream&index,char id[8]);
void delete_student_by_name(fstream &file,fstream &index,fstream &sename,fstream &inverted_name,char name[])
{
sename.seekg(0);
index.seekg(0);
file.seekg(0);
inverted_name.seekg(0);
format(name);
cout <<" NAME "<<name<<"\n";
cout<<"student id "<<binary_search_secondary_name(name)<<"\n";
if(binary_search_secondary_name(name)!=-1)
{
short x=place_secondary_name(name);
cout<<" x "<<secondary_name[x].inverted<<" ";
if(secondary_name[x].inverted!=-1)
{
short x=place_secondary_name(name);
cout<<" x "<<secondary_name[x].inverted<<" ";
if(secondary_name[x].inverted!=-1)
{
char id[8];
id[8]='\0';
inverted_name.seekg(secondary_name[x].inverted);
inverted_name.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
delete_student_by_ID(file,index,id);
//file.close();index.close();inverted_name.close();
while (true)
{
inverted_name.read((char*)&x,sizeof(x));
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
if(x==-1)
{
file.close();
index.close();
inverted_name.close();
sename.close();return;}
inverted_name.seekg(x);
cout<<" x "<<x<<" ";
inverted_name.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_name.eof()){cout <<"error in the list\n";return;}
delete_student_by_ID(file,index,id);
}
secondary_name[x].inverted=-1;
rewrite_index_from_vector_name(sename);
}
else cout <<"There is no student with this name\n";
}
else cout <<"There is no student with this name\n";
file.close();
index.close();
inverted_name.close();
sename.close();
}
}
void delete_student_by_GPA(fstream &file,fstream &index,fstream &seGPA,fstream &inverted_GPA,char GPA[])
{
seGPA.seekg(0);
index.seekg(0);
file.seekg(0);
inverted_GPA.seekg(0);
cout <<" GPA "<<GPA<<"\n";
cout<<"student id "<<binary_search_secondary_GPA(GPA)<<"\n";
if(binary_search_secondary_GPA(GPA)!=-1)
{
short x=place_secondary_GPA(GPA);
cout<<" x "<<secondary_GPA[x].inverted<<" ";
if(secondary_GPA[x].inverted!=-1)
{
short x=place_secondary_GPA(GPA);
cout<<" x "<<secondary_GPA[x].inverted<<" ";
if(secondary_GPA[x].inverted!=-1)
{
char id[8];
id[8]='\0';
inverted_GPA.seekg(secondary_GPA[x].inverted);
inverted_GPA.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_GPA.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
delete_student_by_ID(file,index,id);
//file.close();index.close();inverted_GPA.close();
while (true)
{
inverted_GPA.read((char*)&x,sizeof(x));
if(inverted_GPA.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
if(x==-1)
{
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
inverted_GPA.seekg(x);
cout<<" x "<<x<<" ";
inverted_GPA.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_GPA.eof())
{
file.close();
index.close();
inverted_GPA.close();
seGPA.close();cout <<"error in the list\n";return;
}
delete_student_by_ID(file,index,id);
}
secondary_GPA[x].inverted=-1;
rewrite_index_from_vector_GPA(seGPA);
}
else cout <<"There is no student with this GPA\n";
}
else cout <<"There is no student with this GPA\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();
}
}
void delete_student_by_ID (fstream &file, fstream&index,char id[8])
{
index.seekg(0);
short x;
index.read((char*)&x,sizeof(x));
if(primary_change!=false&&x!=0)
{
index.close();
index.open("index.dat",ios::out|ios::binary|ios::ate);
rewrite_index_vector(index,file);
index.open("index.dat",ios::in|ios::binary);
}
short indexi;x=-1;
file.seekg(0);
indexi=binary_search_primary(id);
if(indexi!=-1)
{
/*short place=place_in_vector(id);
cout<<"place of the id in the vector "<<place<<"\n";
primary[place].ofset=-1;
primary_change=true;//changing in the primary vector
cout <<"place of the id in the index "<<10*place+2<<"\n";
index.seekp(10*place+2,ios::beg);
place=-1;
index.write((char*)&place,sizeof(place));
index.seekp(0);
index.write((char*)&x,sizeof(x));//changing in the index file*/
file.close();
file.open("Student.dat",ios::binary|ios::in);
short head;
file.read((char*)&head,sizeof(head));
//cout<<" delete * "<<head<<" ";
file.close();
if(head==-1)//works fine
{//delete when avail list is empty
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
file.seekp(0);
file.write((char*)&indexi,sizeof(indexi));
file.seekp(indexi+4,ios::beg);//+4 for size of record and id
file.put('*');
file.write((char*)&head,sizeof(head));
short head;
file.seekg(0);
file.read((char*)&head,sizeof(head));
//cout<<" delete * "<<head<<" ";
}
else
{//deleting when having avail list
short size;
//cout <<"the next********* ";
short offset;short myrec;
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
file.seekg(indexi,ios::beg);
file.read((char*)&myrec,sizeof(myrec));//my size
//cout<<"size of my record "<<myrec<<"\n";
file.seekg(head,ios::beg);
file.read((char*)&size,sizeof(size));
//cout <<" size first rec "<<size<<"\n";
if(myrec>=size)//we will put in the beginnig of the list
{//fine
file.seekp(indexi+4,ios::beg);
file.put('*');
file.write((char*)&head,sizeof(head));
//cout <<"the first indexi "<<head<<"\n";
file.seekp(0);
file.write((char*)&indexi,sizeof(indexi));
//cout <<"the head should be "<<indexi<<"\n";
}
else
{
file.seekg(head+5,ios::beg);
file.seekp(head+5,ios::beg);
while (true)
{
file.read((char*)&offset,sizeof(offset));
//cout <<"the next indexi "<<offset<<"\n";
if(file.eof()){cout <<"there is error in the list\n";return;};
if(offset==-1)break;//it will add at end
file.seekg(offset,ios::beg);
file.seekp(offset,ios::beg);
file.read((char*)&size,sizeof(size));
//cout <<"the size "<<size<<"\n";
if(size<myrec)
{
//everything
short place=offset;
offset=0;
file.seekp(indexi+4,ios::beg);
file.put('*');
file.write((char*)&place,sizeof(place));
file.seekg(head+5,ios::beg);
file.seekp(head+5,ios::beg);
while (true)
{
file.read((char*)&offset,sizeof(offset));
if(offset==place)
{
file.seekp(-2,ios::cur);
file.write((char*)&indexi,sizeof(indexi));
}
file.seekg(offset+5,ios::beg);
file.seekp(offset+5,ios::beg);
}
break;
//
}
file.seekg(offset+5,ios::beg);
file.seekp(offset+5,ios::beg);
}
//reach the end of the list
file.seekp(-2,ios::cur);//works fine
//cout <<"we are in "<<file.tellp()<<" ";
file.write((char*)&indexi,sizeof(indexi));
file.seekp(indexi+4,ios::beg);
//cout <<"we are in "<<file.tellp()<<" ";
file.put('*');
short pla=-1;
file.write((char*)&pla,sizeof(pla));
}
}
file.close();
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
x=-1;
index.seekp(0);
index.write((char*)&x,sizeof(x));
primary_change=true;
index.seekp(0);
rewrite_index_vector(index,file);
x=0;
index.seekp(0);
index.write((char*)&x,sizeof(x));
primary_change=false;
}
else
{
cout <<"the student isn't founded\n";
}
file.close();
}
void get_student_by_ID (fstream &file, fstream&index,char id[8])//return maybe student
{
index.seekg(0);
short x;
index.read((char*)&x,sizeof(x));
if(primary_change!=false&&x!=0)
{
index.close();
index.open("index.dat",ios::out|ios::binary|ios::ate);
rewrite_index_vector(index,file);
index.open("index.dat",ios::in|ios::binary);
}
short off=binary_search_primary(id);
if(off!=-1)//either not found or deleted "delete is to put -1 in offset
{
file.seekg(off,ios::beg);
short size,size2,i=0,x=0;//x for the place of short size
file.seekg(off,ios::beg);
//cout <<"I m at "<<file.tellp()<<"\n";
file.read((char*)&size,sizeof(size));
//cout <<"size "<<size<<"\n";
char *record=new char[size];
file.read(record,size);
memcpy (&size2, &record[x], sizeof(size2));
x+=2+size2;
i=x-size2;
cout <<"ID: ";
for(int y=0;y<size2;y++)
{cout <<record[i];i++;}
cout <<"\n";
memcpy (&size2, &record[x], sizeof(size2));
x+=2+size2;
i=x-size2;
cout <<"Name: ";
for(int y=0;y<size2;y++)
{cout <<record[i];i++;}
cout <<"\n";
cout <<"Address : ";
memcpy (&size2, &record[x], sizeof(size2));
x+=2+size2;
i=x-size2;
for(int y=0;y<size2;y++)
{cout <<record[i];i++;}
cout <<"\n";
cout <<"Mobile : ";
memcpy (&size2, &record[x], sizeof(size2));
x+=2+size2;
i=x-size2;
for(int y=0;y<size2;y++)
{cout <<record[i];i++;}
cout <<"\n";
cout <<"GPA : ";
memcpy (&size2, &record[x], sizeof(size2));
x+=2+size2;
i=x-size2;
for(int y=0;y<size2;y++)
{cout <<record[i];i++;}
cout <<"\n";
//file.close();
//file.open("Student.dat",ios::binary|ios::in);
//short head;
//file.read((char*)&head,sizeof(head));
//cout<<" display * "<<head<<" ";
//file.close();
}
else cout <<"There is no student with this id!\n";
//file.close();
}
//49
void get_student_by_name(fstream &file,fstream &sename,fstream &index,fstream &inverted_name,char name[])
{
sename.seekg(0);
index.seekg(0);
file.seekg(0);
inverted_name.seekg(0);
format(name);
cout <<" NAME "<<name<<"\n";
cout<<"student id "<<binary_search_secondary_name(name)<<"\n";
if(binary_search_secondary_name(name)!=-1)
{
/* char n[51];
short x=-1;
sename.seekg(binary_search_secondary_name(name),ios::beg);
sename.read(n,sizeof(n));
cout<<" name "<<n<<"\n";
sename.read((char*)&x,sizeof(x));*/
short x=place_secondary_name(name);
cout<<" x "<<secondary_name[x].inverted<<" ";
if(secondary_name[x].inverted!=-1)
{
char id[8];
id[8]='\0';
inverted_name.seekg(secondary_name[x].inverted);
inverted_name.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
get_student_by_ID(file,index,id);
//file.close();index.close();inverted_name.close();
while (true)
{
inverted_name.read((char*)&x,sizeof(x));
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
if(x==-1)
{
file.close();
index.close();
inverted_name.close();
sename.close();return;}
inverted_name.seekg(x);
cout<<" x "<<x<<" ";
inverted_name.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_name.eof()){cout <<"error in the list\n";return;}
get_student_by_ID(file,index,id);
}
}
else cout <<"There is no student with this name\n";
}
else cout <<"There is no student with this name\n";
file.close();
index.close();
inverted_name.close();
sename.close();
}
void get_student_by_GPA(fstream &file,fstream &seGPA,fstream &index,fstream &inverted_GPA,char GPA[4])
{
seGPA.seekg(0);
index.seekg(0);
file.seekg(0);
inverted_GPA.seekg(0);
cout <<" GPA "<<GPA<<"\n";
cout<<"student id "<<binary_search_secondary_GPA(GPA)<<"\n";
if(binary_search_secondary_GPA(GPA)!=-1)
{
/* char n[51];
short x=-1;
seGPA.seekg(binary_search_secondary_GPA(GPA),ios::beg);
seGPA.read(n,sizeof(n));
cout<<" GPA "<<n<<"\n";
seGPA.read((char*)&x,sizeof(x));*/
short x=place_secondary_GPA(GPA);
cout<<" x "<<secondary_GPA[x].inverted<<" ";
if(secondary_GPA[x].inverted!=-1)
{
char id[8];
id[8]='\0';
inverted_GPA.seekg(secondary_GPA[x].inverted);
inverted_GPA.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_GPA.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
get_student_by_ID(file,index,id);
//file.close();index.close();inverted_GPA.close();
while (true)
{
inverted_GPA.read((char*)&x,sizeof(x));
if(inverted_GPA.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
if(x==-1)
{
file.close();
index.close();
inverted_GPA.close();
seGPA.close();return;}
inverted_GPA.seekg(x);
cout<<" x "<<x<<" ";
inverted_GPA.read(id,8);
cout<<" ID "<<id<<" ";
if(inverted_GPA.eof()){cout <<"error in the list\n";return;}
get_student_by_ID(file,index,id);
}
}
else cout <<"There is no student with this GPA\n";
}
else cout <<"There is no student with this GPA\n";
file.close();
index.close();
inverted_GPA.close();
seGPA.close();
}
struct name_id
{
char name[50];
char id[8];
};
struct gpa_id
{
char gpa[4];
char id[8];
};
void composite_search(fstream &file,fstream &index,fstream &sename,fstream &seGPA,fstream &inverted_name,fstream &inverted_gpa,char name[50],char GPA[4])
{
vector<name_id>nam_id;
vector<gpa_id>gp_id;
/*short i=binary_search_secondary_name(name);
if(i!=-1&&secondary_name[i].inverted!=-1)
{
name_id nm;
char id[8];
id[8]='\0';
inverted_name.seekg(secondary_name[i].inverted);
inverted_name.read(id,8);
strcpy(nm.id,id);
strcpy(nm.name,secondary_name[i].name);
nam_id.push_back(nm);
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
while (true)
{
short x;
name_id nma;
inverted_name.read((char*)&x,sizeof(x));
if(inverted_name.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_name.close();
sename.close();return;}
if(x==-1)
{
break;
/*file.close();
index.close();
inverted_name.close();
sename.close();return;}
inverted_name.seekg(x);
inverted_name.read(id,8);
strcpy(nma.id,id);
strcpy(nma.name,secondary_name[i].name);
nam_id.push_back(nma);
if(inverted_name.eof()){cout <<"error in the list\n";return;}
}
}
else
{
cout <<"there is no student with this name \n";
seGPA.close();
inverted_name.close();
inverted_gpa.close();
sename.close();
index.close();
file.close();
return;
}*/
file.seekg(0);
index.seekg(0);
inverted_gpa.seekg(0);
seGPA.seekg(0);
short i=binary_search_secondary_GPA(GPA);
if(i!=-1&&secondary_GPA[i].inverted!=-1)
{
gpa_id nm;
char id[8];
id[8]='\0';
inverted_gpa.seekg(secondary_GPA[i].inverted);
inverted_gpa.read(id,8);
strcpy(nm.id,id);
strcpy(nm.gpa,secondary_GPA[i].GPA);
gp_id.push_back(nm);
if(inverted_gpa.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_gpa.close();
seGPA.close();return;}
while (true)
{
short x;
gpa_id nma;
inverted_gpa.read((char*)&x,sizeof(x));
if(inverted_gpa.eof())
{cout <<"error in the list\n";
file.close();
index.close();
inverted_gpa.close();
seGPA.close();return;}
if(x==-1)
{
break;
/*file.close();
index.close();
inverted_name.close();
sename.close();return;*/}
inverted_gpa.seekg(x);
inverted_gpa.read(id,8);
strcpy(nma.id,id);
strcpy(nma.gpa,secondary_GPA[i].GPA);
gp_id.push_back(nma);
if(inverted_gpa.eof()){cout <<"error in the list\n";return;}
}
}
else
{
cout <<"there is no student with this GPA \n";
seGPA.close();
inverted_name.close();
inverted_gpa.close();
sename.close();
index.close();
file.close();
return;
}
for(int i=0;i<gp_id.size();i++)
{
//cout <<"the GPA \n"<<gp_id[i].gpa<<"\n";
cout<<gp_id[i].id<"\n";
}
/*for(int i=0;i<nam_id.size();i++)
{
cout <<"the names \n"<<nam_id[i].name<<"\n"<<nam_id[i].id<"\n";
}*/
//sort
for(int i=0;i<nam_id.size();i++)
{
for(int y=0;y<nam_id.size();y++)
{
if(compare(nam_id[i].id,nam_id[y].id)==1)
swap(nam_id[i].id,nam_id[y].id);
}
}
for(int i=0;i<gp_id.size();i++)
{
for(int y=0;y<gp_id.size();y++)
{
if(compare(gp_id[i].id,gp_id[y].id)==1)
swap(gp_id[i].id,gp_id[y].id);
}
}
/*int n=0,g=0;
if(nam_id.size()==0||gp_id.size()==0)
{
seGPA.close();
inverted_name.close();
inverted_gpa.close();
sename.close();
index.close();
file.close();
return;
}
while(true)
{
if(n<nam_id.size())
cout <<nam_id[n].id<<"\n";
if(g<gp_id.size())
cout<<gp_id[g].id<<"\n";
if(compare(nam_id[n].id,gp_id[g].id)==1||n>=nam_id.size())
g++;
else if(compare(nam_id[n].id,gp_id[g].id)==-1||g>=gp_id.size())
n++;
else
{
get_student_by_ID(file,index,nam_id[n].id);
n++;g++;
}
if(n>=nam_id.size()&&g>=gp_id.size())
{
seGPA.close();
inverted_name.close();
inverted_gpa.close();
sename.close();
index.close();
file.close();
return;
}
}*/
}
void updatestudent_byID(fstream &file,fstream &index,fstream& sename,fstream& seGPA,fstream&inverlist_name,fstream&invertlist_GPA, char id[8])//fine
{
index.seekg(0);
short x;
index.read((char*)&x,sizeof(x));
if(primary_change!=false&&x!=0)
{
index.close();
index.open("index.dat",ios::out|ios::binary|ios::ate);
rewrite_index_vector(index,file);
index.open("index.dat",ios::in|ios::binary);
}
cin.ignore();
short size,size2;
student s=cinstudent();
short sizerec=strlen(s.ID)+strlen(s.Name)+strlen(s.Address)+strlen(s.Mobile)
+strlen(s.GPA)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size);
short in=binary_search_primary(id);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
//cout<<"index "<<in<<"\n";
file.seekg(in,ios::beg);
//cout <<"tell g "<<file.tellg()<<"\n";
file.read((char*)&size2,sizeof(size2));
//cout<<"sizerec "<<sizerec<<"size id "<<size2<<"\n";
short pla=place_in_vector(id);
if(pla!=-1)
{
if(sizerec==size2)//updated equal the original size
{
int i=0;
file.seekp(in);
char *record=new char[sizerec];
size=strlen(s.ID);
memcpy (&record[0], &size, sizeof(size));//2
i+=sizeof(sizerec);
int y;
y=0;int z;
for(z=i;z<strlen(s.ID)+i;z++)
{
record[z]=s.ID[y];
y++;
}
i+=size;
size=strlen(s.Name);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Name)+i;z++)
{
record[z]=s.Name[y];
y++;
}
i+=size;
size=strlen(s.Address);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Address)+i;z++)
{
record[z]=s.Address[y];
y++;
}
i+=size;
size=strlen(s.Mobile);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Mobile)+i;z++)
{
record[z]=s.Mobile[y];
y++;
}
i+=size;
size=strlen(s.GPA);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.GPA)+i;z++)
{
record[z]=s.GPA[y];
y++;
}
i+=size;
file.seekp(in);
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
if(!isequal(id,s.ID))
{
short x=-1;
primary_change=true;
index.seekp(0);
index.write((char*)&x,sizeof(x));
short invec=place_in_vector(id);
strcpy(primary[invec].id,s.ID);
sort_primary(0,primary.size());
rewrite_indexfromvector(index);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
x=0;
primary_change=false;
index.seekp(0);
index.write((char*)&x,sizeof(x));
}
}//no change in index
else
{
delete_student_by_ID(file,index,id);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
addstudent(file,index,sename,seGPA,inverlist_name,invertlist_GPA,s);
}
}
else cout <<"No student with this id\n";
index.close();
file.close();
}
void updatestudent_byname(fstream &file,fstream &index,fstream& sename,fstream& seGPA,fstream&inverlist_name,fstream&invertlist_GPA, char name[50])//fine
{
index.seekg(0);
short x;
sename.read((char*)&x,sizeof(x));
if(secondary_name_change!=false&&x!=0)
{
sename.close();
sename.open("sename.dat",ios::out|ios::binary|ios::ate);
//rewrite_sename_vector(sename,file);
sename.open("sename.dat",ios::in|ios::binary);
}
cin.ignore();
short size,size2;
student s=cinstudent();
short sizerec=strlen(s.ID)+strlen(s.Name)+strlen(s.Address)+strlen(s.Mobile)
+strlen(s.GPA)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size);
short in=binary_search_secondary_name(name);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
//cout<<"sename "<<in<<"\n";
file.seekg(in,ios::beg);
//cout <<"tell g "<<file.tellg()<<"\n";
file.read((char*)&size2,sizeof(size2));
//cout<<"sizerec "<<sizerec<<"size id "<<size2<<"\n";
short pla=place_secondary_name(name);
if(pla!=-1)
{
if(sizerec==size2)//updated equal the original size
{
int i=0;
file.seekp(in);
char *record=new char[sizerec];
size=strlen(s.ID);
memcpy (&record[0], &size, sizeof(size));//2
i+=sizeof(sizerec);
int y;
y=0;int z;
for(z=i;z<strlen(s.ID)+i;z++)
{
record[z]=s.ID[y];
y++;
}
i+=size;
size=strlen(s.Name);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Name)+i;z++)
{
record[z]=s.Name[y];
y++;
}
i+=size;
size=strlen(s.Address);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Address)+i;z++)
{
record[z]=s.Address[y];
y++;
}
i+=size;
size=strlen(s.Mobile);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Mobile)+i;z++)
{
record[z]=s.Mobile[y];
y++;
}
i+=size;
size=strlen(s.GPA);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.GPA)+i;z++)
{
record[z]=s.GPA[y];
y++;
}
i+=size;
file.seekp(in);
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
/*if(!isequal(name,s.Name))
{
}*/
}//no change in sename
else
{
delete_student_by_name(file,index,sename,inverlist_name,name);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
addstudent(file,index,sename,seGPA,inverlist_name,invertlist_GPA,s);
}
}
else cout <<"No student with this id\n";
index.close();
seGPA.close();
inverlist_name.close();
invertlist_GPA.close();
sename.close();
file.close();
}
void updatestudent_byGPA(fstream &file,fstream &index,fstream& sename,fstream& seGPA,fstream&inverlist_name,fstream&invertlist_GPA, char GPA[4])//fine
{
index.seekg(0);
short x;
seGPA.read((char*)&x,sizeof(x));
if(secondary_GPA_change!=false&&x!=0)
{
seGPA.close();
seGPA.open("seGPA.dat",ios::out|ios::binary|ios::ate);
//rewrite_seGPA_vector(seGPA,file);
seGPA.open("seGPA.dat",ios::in|ios::binary);
}
cin.ignore();
short size,size2;
student s=cinstudent();
short sizerec=strlen(s.ID)+strlen(s.Name)+strlen(s.Address)+strlen(s.Mobile)
+strlen(s.GPA)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size)+sizeof(size);
short in=binary_search_secondary_GPA(GPA);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
//cout<<"seGPA "<<in<<"\n";
file.seekg(in,ios::beg);
//cout <<"tell g "<<file.tellg()<<"\n";
file.read((char*)&size2,sizeof(size2));
//cout<<"sizerec "<<sizerec<<"size id "<<size2<<"\n";
short pla=place_secondary_GPA(GPA);
if(pla!=-1)
{
if(sizerec==size2)//updated equal the original size
{
int i=0;
file.seekp(in);
char *record=new char[sizerec];
size=strlen(s.ID);
memcpy (&record[0], &size, sizeof(size));//2
i+=sizeof(sizerec);
int y;
y=0;int z;
for(z=i;z<strlen(s.ID)+i;z++)
{
record[z]=s.ID[y];
y++;
}
i+=size;
size=strlen(s.Name);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Name)+i;z++)
{
record[z]=s.Name[y];
y++;
}
i+=size;
size=strlen(s.Address);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Address)+i;z++)
{
record[z]=s.Address[y];
y++;
}
i+=size;
size=strlen(s.Mobile);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.Mobile)+i;z++)
{
record[z]=s.Mobile[y];
y++;
}
i+=size;
size=strlen(s.GPA);
memcpy (&record[i], &size, sizeof(size));
i+=sizeof(sizerec);
y=0;
for(int z=i;z<strlen(s.GPA)+i;z++)
{
record[z]=s.GPA[y];
y++;
}
i+=size;
file.seekp(in);
file.write((char*)&sizerec,sizeof(sizerec));
file.write(record,sizerec);
/*if(!isequal(name,s.Name))
{
}*/
}//no change in seGPA
else
{
delete_student_by_name(file,index,seGPA,inverlist_name,GPA);
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
addstudent(file,index,sename,seGPA,inverlist_name,invertlist_GPA,s);
}
}
else cout <<"No student with this id\n";
index.close();
seGPA.close();
inverlist_name.close();
invertlist_GPA.close();
sename.close();
file.close();
}
void print_all_records(fstream &file,fstream &index)
{
index.seekg(0);
short x;
index.read((char*)&x,sizeof(x));
if(primary_change!=false&&x!=0)
{
index.close();
index.open("index.dat",ios::out|ios::binary|ios::ate);
rewrite_index_vector(index,file);
index.open("index.dat",ios::in|ios::binary);
}
print_primary();
index.seekg(0);
for(int i=0;i<primary.size();i++)
{
if(primary[i].ofset!=-1)
{
get_student_by_ID(file,index,primary[i].id);
}
}
index.close();
file.close();
}
int main()
{
fstream file ("Student.dat",ios::binary|ios::out|ios::app);//write head
if(file.tellp()==0)
{short x=-1;
file.write((char*)&x,sizeof(x));}
fstream index("index.dat",ios::out|ios::binary);//write head
if(index.tellp()==0)
{short x=0;
index.write((char*)&x,sizeof(x));}
fstream sename("sename.dat",ios::out|ios::binary);//write head
if(sename.tellp()==0)
{short x=0;
sename.write((char*)&x,sizeof(x));}
fstream seGPA("seGPA.dat",ios::out|ios::binary);//write head
if(seGPA.tellp()==0)
{short x=0;
seGPA.write((char*)&x,sizeof(x));}
fstream invertlist_name("invertlist_name.dat",ios::out|ios::binary|ios::app);
fstream invertlist_GPA("invertlist_GPA.dat",ios::out|ios::binary|ios::app);
file.close();
sename.close();
seGPA.close();
index.close();
invertlist_name.close();
invertlist_GPA.close();
int choice;
do
{//8 9 upadate
cout <<"Students Management System\n";
cout <<"1)Add New Student\n";
cout <<"2)Update Student By Id\n";
cout <<"3)Delete Student By Id\n";
cout <<"4)Display Student By Id\n";
cout <<"5)Display All Students\n";
cout <<"6)Display By Name\n";
cout <<"7)Display By GPA\n";
cout <<"10)delete by name\n";
cout <<"11)delete by gpa\n";
cout <<"12)Composite Search\n";
cout <<"20)Exit\n";
cin>>choice;
cin.ignore();
if(choice==1)
{//add
student s=cinstudent();
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
sename.open("sename.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_name.open("invertlist_name.dat",ios::binary|ios::out|ios::in|ios::ate);
seGPA.open("seGPA.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_GPA.open("invertlist_GPA.dat",ios::binary|ios::out|ios::in|ios::ate);
addstudent(file,index,sename,seGPA,invertlist_name,invertlist_GPA,s);
//cout <<" gfhfh ";
print_secondary_name();
print_secondary_GPA();
}
else if(choice==2)
{
//update
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
sename.open("sename.dat",ios::binary|ios::out|ios::in|ios::ate);
seGPA.open("seGPA.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_name.open("invertlist_name.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_GPA.open("invertlist_GPA.dat",ios::binary|ios::out|ios::in|ios::ate);
char arr[8];
cout<<"Enter the ID of this student : ";
cin>>arr;
updatestudent_byID(file,index,sename,seGPA,invertlist_name,invertlist_GPA,arr);
}
else if(choice==3)
{//delete
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
char arr[8];
cout<<"Enter the ID of this student : ";
cin>>arr;
delete_student_by_ID(file,index,arr);
}
else if(choice==4)
{//display
file.open("Student.dat",ios::binary|ios::in);
index.open("index.dat",ios::binary|ios::in);
char arr[8];
cout<<"Enter the ID of this student : ";
cin>>arr;
get_student_by_ID(file,index,arr);
}
else if (choice==5)
{//print all records
print_primary();
file.open("Student.dat",ios::binary|ios::in);
index.open("index.dat",ios::binary|ios::in);
print_all_records(file,index);
}
else if(choice==6)
{
file.close();index.close();
file.open("Student.dat",ios::binary|ios::in);
index.open("index.dat",ios::binary|ios::in);
sename.open("sename.dat",ios::binary|ios::in);
invertlist_name.open("invertlist_name.dat",ios::binary|ios::in);
char arr[50];
cout<<"Enter the name of this student : ";
cin>>arr;
get_student_by_name(file,sename,index,invertlist_name,arr);
cout <<"the stupid \n";
print_secondary_name();
}
else if(choice==7)
{
file.close();index.close();
file.open("Student.dat",ios::binary|ios::in);
index.open("index.dat",ios::binary|ios::in);
seGPA.open("seGPA.dat",ios::binary|ios::in);
invertlist_GPA.open("invertlist_GPA.dat",ios::binary|ios::in);
char arr[4];
cout<<"Enter the GPA of this student : ";
cin>>arr;
arr[4]='\0';
get_student_by_GPA(file,seGPA,index,invertlist_GPA,arr);
cout <<"the stupid \n";
print_secondary_GPA();
}
else if(choice==8)
{
}
else if(choice==10)
{
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
sename.open("sename.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_name.open("invertlist_name.dat",ios::binary|ios::out|ios::in|ios::ate);
char arr[50];
cout<<"Enter the name of this student : ";
cin>>arr;
format(arr);
delete_student_by_name(file,index,sename,invertlist_name,arr);
}
else if(choice==11)
{
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
seGPA.open("seGPA.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_GPA.open("invertlist_GPA.dat",ios::binary|ios::out|ios::in|ios::ate);
char arr[4];
cout<<"Enter the GPA of this student : ";
cin>>arr;
arr[4]='\0';
delete_student_by_GPA(file,index,seGPA,invertlist_GPA,arr);
}
else if (choice==12)
{
file.open("Student.dat",ios::binary|ios::out|ios::in|ios::ate);
index.open("index.dat",ios::binary|ios::out|ios::in|ios::ate);
sename.open("sename.dat",ios::binary|ios::out|ios::in|ios::ate);
seGPA.open("seGPA.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_name.open("invertlist_name.dat",ios::binary|ios::out|ios::in|ios::ate);
invertlist_GPA.open("invertlist_GPA.dat",ios::binary|ios::out|ios::in|ios::ate);
char GPA[4];char Name[50];
cout<<"Enter the name : ";
cin>>Name;
Name[50]='\0';
cout <<"Enter the GPA : ";
cin>>GPA;
GPA[4]='\0';
composite_search(file,index,sename,seGPA,invertlist_name,invertlist_GPA,Name,GPA);
}
}
while(choice!=20);
return 0;
}
/*
notes
1. in function rewrite index vector complete it -it still needs if the vector was wrong
2. retrieving data from files
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment