• Forum
  • Jobs
  • Why does my SaveToFile function not work

 
Why does my SaveToFile function not work?

I try to save my input to a text file in a format which each information is divided by a comma. But it doesn't work. What mistake did I make here? What should I do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void SaveToFile(string filename) {
	ofstream f("employee.txt");
	Employee *emp;
	Manager* man;
	f.open("employee.txt", std::ios_base::binary | std::ios_base::app);
	for (int i = 0; i < list.size(); i++) {
		emp = list.get(i);
		man = dynamic_cast<Manager*>(emp);
		if (f.is_open())
		{
			f.write(reinterpret_cast<char*>(&list), sizeof(list));
			f << emp->getMaSo() << "," << emp->getTen() << "," <<
				emp->getNgaySinh() << "," << emp->getEmail() << "," <<
				emp->getDienThoai() << "," << emp->getDiaChi() << "," <<
				emp->getLuongCoBan() << "," << man->getChucVu() << ","
				<< man->getHeSoChucVu() << "," << man->getThuong() << endl;
		}
	}

	f.close();
}


This is my employee file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "employee.h"

using namespace std;

Employee::Employee() {
	ma_so_ = "";//id
	ten_ = "";//Name
	ngay_sinh_ = "";//Birth
	email_ = "";
	dien_thoai_ = "";//Phone
	dia_chi_ = "";//Address
	luong_co_ban_ = 0.0;//Salary

}

Employee::~Employee() {

}

void Employee::NhapThongTin() {
	cin.ignore();
	cout << "Nhap thong tin mot nhan vien moi\n";
	cout << "Ma so:";
	getline(cin, ma_so_);
	cout << "Ho & Ten:";
	getline(cin, ten_);
	cout << "Ngay sinh:";
	getline(cin, ngay_sinh_);
	cout << "Email:";
	getline(cin, email_);
	cout << "Dien thoai:";
	getline(cin, dien_thoai_);
	cout << "Dia chi:";
	getline(cin, dia_chi_);
	cout << "Luong co ban:";
	cin >> luong_co_ban_;
}

void Employee::XuatThongTin() {
	cout << "Ma so: " << ma_so_ << endl;
	cout << "Ho & Ten: " << ten_ << endl;
	cout << "Ngay sinh: " << ngay_sinh_ << endl;
	cout << "Email: " << email_ << endl;
	cout << "Dien thoai: " << dien_thoai_ << endl;
	cout << "Dia chi: " << dia_chi_ << endl;
}

float Employee::TinhLuong() {
	return luong_co_ban_;
}

const std::string& Employee::getDiaChi() const {
	return dia_chi_;
}

void Employee::setDiaChi(const std::string& diaChi) {
	dia_chi_ = diaChi;
}

const std::string& Employee::getDienThoai() const {
	return dien_thoai_;
}

void Employee::setDienThoai(const std::string& dienThoai) {
	dien_thoai_ = dienThoai;
}

const std::string& Employee::getEmail() const {
	return email_;
}

void Employee::setEmail(const std::string& email) {
	email_ = email;
}

float Employee::getLuongCoBan() const {
	return luong_co_ban_;
}

void Employee::setLuongCoBan(float luongCoBan) {
	luong_co_ban_ = luongCoBan;
}

const std::string& Employee::getMaSo() const {
	return ma_so_;
}

void Employee::setMaSo(const std::string& maSo) {
	ma_so_ = maSo;
}

const std::string& Employee::getNgaySinh() const {
	return ngay_sinh_;
}

void Employee::setNgaySinh(const std::string& ngaySinh) {
	ngay_sinh_ = ngaySinh;
}

const std::string& Employee::getTen() const {
	return ten_;
}

void Employee::setTen(const std::string& ten) {
	ten_ = ten;
}


and my Manager file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "manager.h"

using namespace std;

Manager::Manager() :
		Employee() {
	chuc_vu_ = "";//roll
	he_so_chuc_vu_ = 0.0;//roll hierarchy
	thuong_ = 0.0;
}

Manager::~Manager() {

}

void Manager::NhapThongTin() {
	Employee::NhapThongTin();
	cin.ignore();
	cout << "Chuc vu:";
	getline(cin, chuc_vu_);
	cout << "He so chu vu:";
	cin >> he_so_chuc_vu_;
	cout << "Thuong:";
	cin >> thuong_;
}

float Manager::TinhLuong() {
	return luong_co_ban_ + luong_co_ban_ * he_so_chuc_vu_ + thuong_;
}

void Manager::XuatThongTin() {
	Employee::XuatThongTin();
	cout << "Chuc vu: " << chuc_vu_ << endl;
	cout << "Luong: " << TinhLuong() << endl;
}

const std::string& Manager::getChucVu() const {
	return chuc_vu_;
}

void Manager::setChucVu(const std::string& chucVu) {
	chuc_vu_ = chucVu;
}

float Manager::getHeSoChucVu() const {
	return he_so_chuc_vu_;
}

void Manager::setHeSoChucVu(float heSoChucVu) {
	he_so_chuc_vu_ = heSoChucVu;
}

float Manager::getThuong() const {
	return thuong_;
}

void Manager::setThuong(float thuong) {
	thuong_ = thuong;
}


And my list file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "list.h"
#include "employee.h"

template <class T>
List<T>::List() {
	index = 0;
}

template <typename T>
List<T>::~List() {

}

template<class T>
void List<T>::add(T const& item) {
	elements_[index] = item;
	index++;
}

template<class T>
T List<T>::get(int pos) {
	int n = sizeof(elements_) / sizeof(elements_[0]);
	if (pos < 0 || pos >= n)
		throw std::out_of_range("error");
	return elements_[pos];
}

template<class T>
int List<T>::size() {
	if (index == 0)
	{
		return 0;
	}
	else
	{
		return index;
	}
}
Last edited on
Why is this in the jobs section?

> f.open("employee.txt", std::ios_base::binary | std::ios_base::app);
Get rid of this, because you've already opened the file.

> f.write(reinterpret_cast<char*>(&list), sizeof(list));
Get rid of this, because it's wrong in so many ways.
Just stick to the f << approach.
#salem c
You're right. It works perfectly, but could you explain the second one for me? Why that cast matter?
Last edited on
Well the first thing you need to do is convince yourself that sizeof(list) is a constant, and has absolutely no bearing on the number of elements in the list.

The only thing you can reliably use f.write with are POD types.
https://en.cppreference.com/w/cpp/named_req/PODType

> Why that cast matter?
Because C++ doesn't have a void* like C does that just means "here are some bytes".
#salem c
Thanks a lot!
Topic archived. No new replies allowed.