EDN Admin
Well-known member
Hi everybody.
Im getting trouble with reading from binary file. My intention is like this:
Suppose that there has already existed a binary file, which containing 3 students, and per student contain Student ID, name, birthday, class. Now I want to read that binary file, and output its content to screen, just like when I input those
3 students, and output right to the screen. Here is my code:
this is QLSach class, which manage a list of students
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <span style="color:#A31515; "NgayThang.h"
#include <ctime>
#include <Windows.h>
#include <string.h>
#include <fstream>
#include <cstdlib>
<span style="color:Blue; class QLSach : <span style="color:Blue; protected SinhVien, <span style="color:Blue; public Sach
{
<span style="color:Blue; private:
SinhVien *ds; <span style="color:Green; //a list of student
<span style="color:Blue; int SoLuongSV; <span style="color:Green; //number of students
Sach *dssach;
<span style="color:Blue; int SoLuongSach;
NgayThang NgayMuon, NgayTra;
<span style="color:Blue; static <span style="color:Blue; int size;
<span style="color:Blue; public:
QLSach() : SinhVien(), Sach()
{
time_t now = time(0);
cout << asctime(localtime(&now));
<span style="color:Green; //ds = new SinhVien[SoLuongSV];
fstream file(<span style="color:#A31515; "E:\DSSV.txt", ios::<span style="color:Blue; in|ios::ate|ios::binary);
<span style="color:Blue; if(!file.good())
{
SoLuongSV = 0;
}
<span style="color:Blue; else
{
SoLuongSV = file.tellg();
}
}
<span style="color:Blue; void ThemDSSV();
<span style="color:Blue; void ThemDSSach();
<span style="color:Blue; void XuatDSSV();
<span style="color:Blue; void XuatDSSach();
<span style="color:Blue; void MuonSach(SinhVien a, Sach b);
};
<span style="color:Blue; int QLSach::size = <span style="color:Blue; sizeof(SinhVien);
<span style="color:Blue; void QLSach::ThemDSSV()
{
ofstream output(<span style="color:#A31515; "E:\DSSV.txt", ios::app|ios::binary);
<span style="color:Blue; if(!output)
{
cout<<<span style="color:#A31515; "Khong mo duoc file";
exit(1);
}
<span style="color:Blue; else
{
SinhVien sv;
cout<<<span style="color:#A31515; "Nhap so luong SV can luu thong tin: ";
cin>>SoLuongSV;
ds = <span style="color:Blue; new SinhVien[SoLuongSV];
<span style="color:Blue; for(<span style="color:Blue; int i=0; i<SoLuongSV; ++i)
{
system(<span style="color:#A31515; "cls");
cout<<<span style="color:#A31515; "Sinh vien thu "<<i+1<<endl;
ds = sv.NhapSV();
}
output.write((<span style="color:Blue; char*)&ds, <span style="color:Blue; sizeof(SinhVien)*SoLuongSV);
}
output.close();
}
<span style="color:Blue; void QLSach::XuatDSSV()
{
ifstream input(<span style="color:#A31515; "E:\DSSV.txt", ios::<span style="color:Blue; in|ios::binary);
system(<span style="color:#A31515; "cls");
<span style="color:Blue; if(!input)
{
cout<<<span style="color:#A31515; "Doc file bi loi";
exit(1);
}
<span style="color:Blue; else
{
<span style="color:Green; //input.read(reinterpret_cast<char*>(ds), sizeof(ds));
<span style="color:Blue; for(<span style="color:Blue; int i=0; i<SoLuongSV; i++)
{
input.seekg((i)*size);
input.read((<span style="color:Blue; char*)&ds, size);
ds.XuatSV(ds);
}
}
input.close();
}
[/code]
this is SinhVien class, which contain information of a student
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <iostream>
#include <string.h>
#include <span style="color:#A31515; "NgayThang.h" <span style="color:Green; //date class
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; class SinhVien
{
<span style="color:Blue; protected:
<span style="color:Blue; char *MSSV, *HoTen, *LopHoc; <span style="color:Green; //ID, name, class
NgayThang NgaySinh; <span style="color:Green; //date
<span style="color:Blue; int SoSachMuon;
<span style="color:Blue; public:
SinhVien()
{
NgayThang::NgayThang();
MSSV = <span style="color:#A31515; "Chua biet";
HoTen = <span style="color:#A31515; "Chua biet";
LopHoc = <span style="color:#A31515; "Chua biet";
}
SinhVien NhapSV(); <span style="color:Green; //student input
<span style="color:Blue; void XuatSV(SinhVien a); <span style="color:Green; //student output
};
SinhVien SinhVien::NhapSV()
{
SinhVien a;
cout<<<span style="color:#A31515; "Nhap MSSV: ";
a.MSSV = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.MSSV);
cout<<<span style="color:#A31515; "Nhap Ho Ten: ";
a.HoTen = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.HoTen);
cout<<<span style="color:#A31515; "Nhap Ngay thang nam sinh "<<endl;
a.NgaySinh.Nhap();
cout<<<span style="color:#A31515; "Nhap Lop Hoc: ";
a.LopHoc = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.LopHoc);
<span style="color:Blue; return a;
}
<span style="color:Blue; void SinhVien::XuatSV(SinhVien a)
{
cout<<<span style="color:#A31515; "MSSV: "<<a.MSSV<<endl;
cout<<<span style="color:#A31515; "Ho Ten: "<<a.HoTen<<endl;
cout<<<span style="color:#A31515; "Ngay sinh: ";
a.NgaySinh.Xuat();
cout<<endl<<<span style="color:#A31515; "Lop: "<<a.LopHoc<<endl;
}
[/code]
this is NgayThang class, which define day, month and year
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <iostream>
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; class NgayThang
{
<span style="color:Blue; protected:
<span style="color:Blue; int Ngay, Thang, Nam;
<span style="color:Blue; public:
NgayThang()
{
Ngay = 0;
Thang = 0;
Nam = 0;
}
<span style="color:Green; /*int GetNgay();
int GetThang();
int GetNam();*/
<span style="color:Blue; void Nhap(); <span style="color:Green; //input
<span style="color:Blue; void Xuat(); <span style="color:Green; //output
<span style="color:Blue; friend ostream &<span style="color:Blue; operator<<(ostream &output, <span style="color:Blue; const NgayThang &ngaythang);
<span style="color:Blue; friend istream &<span style="color:Blue; operator>>(istream &input, NgayThang &ngaythang);
};
<span style="color:Blue; void NgayThang::Nhap()
{
cout<<<span style="color:#A31515; "Nhap Ngay: ";
cin>>Ngay;
cout<<<span style="color:#A31515; "Nhap Thang: ";
cin>>Thang;
cout<<<span style="color:#A31515; "Nhap Nam: ";
cin>>Nam;
}
<span style="color:Blue; void NgayThang::Xuat()
{
cout<<Ngay<<<span style="color:#A31515; "/"<<Thang<<<span style="color:#A31515; "/"<<Nam;
}
ostream &<span style="color:Blue; operator<<(ostream &output, <span style="color:Blue; const NgayThang &ngaythang)
{
<span style="color:Blue; return output<<ngaythang.Ngay<<<span style="color:#A31515; "/"<<ngaythang.Thang<<<span style="color:#A31515; "/"<<ngaythang.Nam;
}
istream &<span style="color:Blue; operator>>(istream &input, NgayThang &ngaythang)
{
cout<<<span style="color:#A31515; "Nhap Ngay: ";
input>>ngaythang.Ngay;
cout<<<span style="color:#A31515; "Nhap Thang: ";
input>>ngaythang.Thang;
cout<<<span style="color:#A31515; "Nhap Nam: ";
input>>ngaythang.Nam;
<span style="color:Blue; return input;
}
<span style="color:Green; //int NgayThang::GetNgay()
<span style="color:Green; //{
<span style="color:Green; // return Ngay;
<span style="color:Green; //}
<span style="color:Green; //
<span style="color:Green; //int NgayThang::GetThang()
<span style="color:Green; //{
<span style="color:Green; // return Thang;
<span style="color:Green; //}
<span style="color:Green; //
<span style="color:Green; //int NgayThang::GetNam()
<span style="color:Green; //{
<span style="color:Green; // return Nam;
<span style="color:Green; //}
[/code]
This is main function:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // QLTV_LTHDT.cpp : main project file.
#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "SinhVien.h"
#include <span style="color:#A31515; "Sach.h"
#include <span style="color:#A31515; "QLSach.h"
#include <span style="color:#A31515; "NgayThang.h"
#include <conio.h>
<span style="color:Blue; using <span style="color:Blue; namespace System;
<span style="color:Blue; int main(<span style="color:Blue; array<System::String ^> ^args)
{
QLSach a;
<span style="color:Green; //a.ThemDSSV();
a.XuatDSSV();
<span style="color:Green; /*NgayThang a;
cout<<"Nhap ngay thang nam: ";
cin>>a;*/
<span style="color:Green; //cout<<a;
getch();
<span style="color:Blue; return 0;
}
[/code]
Above is my source code.But when I start debug, it occurs the unhandling exception that I dont understand. I wonder how to fix it when there are no error display T___T Anybody can help me, please ?
View the full article
Im getting trouble with reading from binary file. My intention is like this:
Suppose that there has already existed a binary file, which containing 3 students, and per student contain Student ID, name, birthday, class. Now I want to read that binary file, and output its content to screen, just like when I input those
3 students, and output right to the screen. Here is my code:
this is QLSach class, which manage a list of students
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <span style="color:#A31515; "NgayThang.h"
#include <ctime>
#include <Windows.h>
#include <string.h>
#include <fstream>
#include <cstdlib>
<span style="color:Blue; class QLSach : <span style="color:Blue; protected SinhVien, <span style="color:Blue; public Sach
{
<span style="color:Blue; private:
SinhVien *ds; <span style="color:Green; //a list of student
<span style="color:Blue; int SoLuongSV; <span style="color:Green; //number of students
Sach *dssach;
<span style="color:Blue; int SoLuongSach;
NgayThang NgayMuon, NgayTra;
<span style="color:Blue; static <span style="color:Blue; int size;
<span style="color:Blue; public:
QLSach() : SinhVien(), Sach()
{
time_t now = time(0);
cout << asctime(localtime(&now));
<span style="color:Green; //ds = new SinhVien[SoLuongSV];
fstream file(<span style="color:#A31515; "E:\DSSV.txt", ios::<span style="color:Blue; in|ios::ate|ios::binary);
<span style="color:Blue; if(!file.good())
{
SoLuongSV = 0;
}
<span style="color:Blue; else
{
SoLuongSV = file.tellg();
}
}
<span style="color:Blue; void ThemDSSV();
<span style="color:Blue; void ThemDSSach();
<span style="color:Blue; void XuatDSSV();
<span style="color:Blue; void XuatDSSach();
<span style="color:Blue; void MuonSach(SinhVien a, Sach b);
};
<span style="color:Blue; int QLSach::size = <span style="color:Blue; sizeof(SinhVien);
<span style="color:Blue; void QLSach::ThemDSSV()
{
ofstream output(<span style="color:#A31515; "E:\DSSV.txt", ios::app|ios::binary);
<span style="color:Blue; if(!output)
{
cout<<<span style="color:#A31515; "Khong mo duoc file";
exit(1);
}
<span style="color:Blue; else
{
SinhVien sv;
cout<<<span style="color:#A31515; "Nhap so luong SV can luu thong tin: ";
cin>>SoLuongSV;
ds = <span style="color:Blue; new SinhVien[SoLuongSV];
<span style="color:Blue; for(<span style="color:Blue; int i=0; i<SoLuongSV; ++i)
{
system(<span style="color:#A31515; "cls");
cout<<<span style="color:#A31515; "Sinh vien thu "<<i+1<<endl;
ds = sv.NhapSV();
}
output.write((<span style="color:Blue; char*)&ds, <span style="color:Blue; sizeof(SinhVien)*SoLuongSV);
}
output.close();
}
<span style="color:Blue; void QLSach::XuatDSSV()
{
ifstream input(<span style="color:#A31515; "E:\DSSV.txt", ios::<span style="color:Blue; in|ios::binary);
system(<span style="color:#A31515; "cls");
<span style="color:Blue; if(!input)
{
cout<<<span style="color:#A31515; "Doc file bi loi";
exit(1);
}
<span style="color:Blue; else
{
<span style="color:Green; //input.read(reinterpret_cast<char*>(ds), sizeof(ds));
<span style="color:Blue; for(<span style="color:Blue; int i=0; i<SoLuongSV; i++)
{
input.seekg((i)*size);
input.read((<span style="color:Blue; char*)&ds, size);
ds.XuatSV(ds);
}
}
input.close();
}
[/code]
this is SinhVien class, which contain information of a student
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <iostream>
#include <string.h>
#include <span style="color:#A31515; "NgayThang.h" <span style="color:Green; //date class
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; class SinhVien
{
<span style="color:Blue; protected:
<span style="color:Blue; char *MSSV, *HoTen, *LopHoc; <span style="color:Green; //ID, name, class
NgayThang NgaySinh; <span style="color:Green; //date
<span style="color:Blue; int SoSachMuon;
<span style="color:Blue; public:
SinhVien()
{
NgayThang::NgayThang();
MSSV = <span style="color:#A31515; "Chua biet";
HoTen = <span style="color:#A31515; "Chua biet";
LopHoc = <span style="color:#A31515; "Chua biet";
}
SinhVien NhapSV(); <span style="color:Green; //student input
<span style="color:Blue; void XuatSV(SinhVien a); <span style="color:Green; //student output
};
SinhVien SinhVien::NhapSV()
{
SinhVien a;
cout<<<span style="color:#A31515; "Nhap MSSV: ";
a.MSSV = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.MSSV);
cout<<<span style="color:#A31515; "Nhap Ho Ten: ";
a.HoTen = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.HoTen);
cout<<<span style="color:#A31515; "Nhap Ngay thang nam sinh "<<endl;
a.NgaySinh.Nhap();
cout<<<span style="color:#A31515; "Nhap Lop Hoc: ";
a.LopHoc = (<span style="color:Blue; char*)malloc(<span style="color:Blue; sizeof(<span style="color:Blue; char));
fflush(stdin);
gets(a.LopHoc);
<span style="color:Blue; return a;
}
<span style="color:Blue; void SinhVien::XuatSV(SinhVien a)
{
cout<<<span style="color:#A31515; "MSSV: "<<a.MSSV<<endl;
cout<<<span style="color:#A31515; "Ho Ten: "<<a.HoTen<<endl;
cout<<<span style="color:#A31515; "Ngay sinh: ";
a.NgaySinh.Xuat();
cout<<endl<<<span style="color:#A31515; "Lop: "<<a.LopHoc<<endl;
}
[/code]
this is NgayThang class, which define day, month and year
<div style="color:Black;background-color:White; <pre>
#pragma once
#include <iostream>
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; class NgayThang
{
<span style="color:Blue; protected:
<span style="color:Blue; int Ngay, Thang, Nam;
<span style="color:Blue; public:
NgayThang()
{
Ngay = 0;
Thang = 0;
Nam = 0;
}
<span style="color:Green; /*int GetNgay();
int GetThang();
int GetNam();*/
<span style="color:Blue; void Nhap(); <span style="color:Green; //input
<span style="color:Blue; void Xuat(); <span style="color:Green; //output
<span style="color:Blue; friend ostream &<span style="color:Blue; operator<<(ostream &output, <span style="color:Blue; const NgayThang &ngaythang);
<span style="color:Blue; friend istream &<span style="color:Blue; operator>>(istream &input, NgayThang &ngaythang);
};
<span style="color:Blue; void NgayThang::Nhap()
{
cout<<<span style="color:#A31515; "Nhap Ngay: ";
cin>>Ngay;
cout<<<span style="color:#A31515; "Nhap Thang: ";
cin>>Thang;
cout<<<span style="color:#A31515; "Nhap Nam: ";
cin>>Nam;
}
<span style="color:Blue; void NgayThang::Xuat()
{
cout<<Ngay<<<span style="color:#A31515; "/"<<Thang<<<span style="color:#A31515; "/"<<Nam;
}
ostream &<span style="color:Blue; operator<<(ostream &output, <span style="color:Blue; const NgayThang &ngaythang)
{
<span style="color:Blue; return output<<ngaythang.Ngay<<<span style="color:#A31515; "/"<<ngaythang.Thang<<<span style="color:#A31515; "/"<<ngaythang.Nam;
}
istream &<span style="color:Blue; operator>>(istream &input, NgayThang &ngaythang)
{
cout<<<span style="color:#A31515; "Nhap Ngay: ";
input>>ngaythang.Ngay;
cout<<<span style="color:#A31515; "Nhap Thang: ";
input>>ngaythang.Thang;
cout<<<span style="color:#A31515; "Nhap Nam: ";
input>>ngaythang.Nam;
<span style="color:Blue; return input;
}
<span style="color:Green; //int NgayThang::GetNgay()
<span style="color:Green; //{
<span style="color:Green; // return Ngay;
<span style="color:Green; //}
<span style="color:Green; //
<span style="color:Green; //int NgayThang::GetThang()
<span style="color:Green; //{
<span style="color:Green; // return Thang;
<span style="color:Green; //}
<span style="color:Green; //
<span style="color:Green; //int NgayThang::GetNam()
<span style="color:Green; //{
<span style="color:Green; // return Nam;
<span style="color:Green; //}
[/code]
This is main function:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // QLTV_LTHDT.cpp : main project file.
#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "SinhVien.h"
#include <span style="color:#A31515; "Sach.h"
#include <span style="color:#A31515; "QLSach.h"
#include <span style="color:#A31515; "NgayThang.h"
#include <conio.h>
<span style="color:Blue; using <span style="color:Blue; namespace System;
<span style="color:Blue; int main(<span style="color:Blue; array<System::String ^> ^args)
{
QLSach a;
<span style="color:Green; //a.ThemDSSV();
a.XuatDSSV();
<span style="color:Green; /*NgayThang a;
cout<<"Nhap ngay thang nam: ";
cin>>a;*/
<span style="color:Green; //cout<<a;
getch();
<span style="color:Blue; return 0;
}
[/code]
Above is my source code.But when I start debug, it occurs the unhandling exception that I dont understand. I wonder how to fix it when there are no error display T___T Anybody can help me, please ?
View the full article