0% found this document useful (0 votes)
2 views

demoFile

The document is a C++ program that defines a structure for student information, including name, average grade, and age. It contains functions to input, output, write, and read student data from a binary file. The main function demonstrates reading student data from a file and displaying it on the console.

Uploaded by

viethoang571994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

demoFile

The document is a C++ program that defines a structure for student information, including name, average grade, and age. It contains functions to input, output, write, and read student data from a binary file. The main function demonstrates reading student data from a file and displaying it on the console.

Uploaded by

viethoang571994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//

===============================
===============================
==============
// Name : DemoFileNhiPhan.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//
===============================
===============================
==============

#include <iostream>
#include<fstream>
#include<string>
using namespace std;
#define FNAME "SinhVien.txt"
struct SinhVien{
string hoTen;//char hoTen[25];
float dtb;
int tuoi;
//ham nhap, xuat thong tin
void nhapSVInfo(){
cout<<"Nhap ho ten: ";
getline(cin, hoTen);
cout<<"Nhap diem TB: ";
cin>>dtb;
cout<<"Nhap tuoi: ";
cin>>tuoi;

}
void hienSVInfo(){
cout<<"\nHo ten: "<<hoTen
<<"\nDTB: "<<dtb<<"\nTuoi:
"<<tuoi<<endl;
}
};

void ghiFile(string fName, SinhVien sv){


//khai file de thuc hien ghi file
ofstream output (fName, ios::binary|ios::out|
ios::app);
//thuc hien ghi file
output.write((char*)&sv, sizeof(SinhVien));
//dong file
output.close();
}
void docFile(string fName, SinhVien dssv[], int
&slg){
ifstream input(fName, ios::binary|ios::in);
//thuc hien doc file
int dem=0;
SinhVien sv;
//thuc hien doc lan dau
input.read((char *) &sv,sizeof(SinhVien));
while(!input.eof()){
dssv[dem]=sv;
dem++;
input.read((char *) &sv,sizeof(SinhVien));
}
slg=dem;
//dong file
input.close();
}

int main() {
/*
SinhVien sv;
cout<<"Nhap thong tin sinh vien: "<<endl;
sv.nhapSVInfo();
cout<<"Ghi thong tin sinh vien vao file:
"<<endl;
ghiFile(FNAME, sv);
*/
SinhVien dssv[10];
int slg;
cout<<"Doc du lieu sinh vien tu file: "<<endl;
docFile(FNAME, dssv, slg);
for(int i=0;i<slg;i++)
dssv[i].hienSVInfo();

return 0;
}

You might also like