Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Đọc ghi 1 mảng cấu trúc trong C++


#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

struct thisinh{
char sobd[15];
char hoten[150];
char diachi[400];
float toan;
float ly;
float hoa;
};
int N=100;//so phan tu toi da
int soTS=0;

int timkiem(thisinh ds[],char * sobd){
for(int i=0;i<soTS;i++){
if(strcmpi(ds[i].sobd,sobd)==0)
return i;
}
return -1;
}
int ThemTS(thisinh ds[],thisinh ts){
if(timkiem(ds,ts.sobd)==-1){
ds[soTS]=ts;
soTS++;
return 1;
}
return -1;
}
void InDS(thisinh ds[]){
for(int i=0;i<soTS;i++){
cout<<"--------------------------------------------------------------\n";
cout<<"So bao danh: "<<ds[i].sobd<<"\t";
cout<<"Ho va ten: "<<ds[i].hoten<<'\t';
cout<<"Dia chi: "<<ds[i].diachi<<'\t';
cout<<"Diem toan: "<<ds[i].toan<<"\tLy:"
<<ds[i].ly<<"\tHoa:"<<ds[i].hoa
<<endl;
}
}

//Sap xep thi sinh theo ten
//su dung giai thuat sap xep doi cho truc tiep
//ds la danh sach thi sinh
//n la so phan tu thuc te co trong danh sach
void interchangeSort(thisinh ds[]){
for(int i=0;i<soTS-1;i++)
for(int j=i+1;j<soTS;j++)
if(strcmpi(ds[i].hoten,ds[j].hoten)==1){
thisinh t=ds[i];
ds[i]=ds[j];
ds[j]=t;
}

}
//ham ghi du lieu xuong file
void write2file(char * filename, thisinh ds[],int n){
ofstream fs(filename, ios::binary | ios::out );//| ios::app
for(int i=0;i<n;i++){
fs.write((char*)&ds[i],sizeof(ds[i]));
}
fs.close();
}
///hàm doc du lieu tu file
void readfromfile(char * filename, thisinh ds[]){
ifstream infile(filename,ios::in|ios::binary);
thisinh ts;
infile.read((char*)&ts,sizeof(thisinh));
soTS=0;
while(!infile.eof()){
ds[soTS]=ts;
soTS++;
infile.read((char*)&ts,sizeof(thisinh));
}
infile.close();
}

int main(){
thisinh ds[N];

cout<<"Loading data...\n";
readfromfile("c:\\data.data",ds);
cout<<"loading completed with "<<soTS<<" records.\n";
do{
cout<<"1. Nhap thong tin thi sinh\n";
cout<<"2. In danh sach thi sinh\n";
cout<<"3.\n";
cout<<"4.\n";
cout<<"5.\n";
cout<<"6.\n";
cout<<"7.\n";
cout<<"8.\n";
cout<<"9.\n";
cout<<"10.\n";
cout<<"11.\n";
cout<<"12.\n";
cout<<"100.Thoat\n";
cout<<"\n***Chon lua cua ban: ";
int tl;cin>>tl;
switch(tl){
case 1:
thisinh ts;
cout<<"Nhap so bao danh: ";gets(ts.sobd);
cout<<"Nhap ho ten thi sinh: ";gets(ts.hoten);
cout<<"Nhap dia chi thi sinh: ";gets(ts.diachi);
cout<<"Diem toan:";cin>>ts.toan;
cout<<"Diem ly:";cin>>ts.ly;
cout<<"Diem hoa:";cin>>ts.hoa;
ThemTS(ds,ts);
break;
case 2:
InDS(ds);
break;
case 100:
write2file("c:\\data.data",ds,soTS);
cout<<"Du lieu da duoc luu vao file.\n Finished";
exit(1);
}
}while(true);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: