#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
struct date{
int day;
int month;
int year;
};
struct employee{
char name[30];
date birthday;
char role[30];
float salary;
};
void main(){
int recordNumber; //So luong ban ghi
cout << "Nhap so luong ban ghi: ";
cin >> recordNumber;
fflush(stdin);
fstream f("A.txt", ios::out | ios::binary);
if (!f){
cout << "Khong mo duoc tep tin" << endl;
exit(1);
}
employee myemployee;
for (int i = 0; i < recordNumber; i++){
cout << "ban ghi thu" << i + 1 << endl;
cout << "Name: ";
cin.getline(myemployee.name, 30);
fflush(stdin);
cout << "Day of birth: ";
cin >> myemployee.birthday.day;
fflush(stdin);
cout << "Month of birth: ";
cin >> myemployee.birthday.month;
fflush(stdin);
cout << "Year of birth: ";
cin >> myemployee.birthday.year;
fflush(stdin);
cout << "Role: ";
cin.getline(myemployee.role, 30);
fflush(stdin);
cout << "Salary: ";
cin >> myemployee.salary;
fflush(stdin);
f.write(reinterpret_cast <char *>(&myemployee), sizeof(employee));
}
f.close();
}
Tại sao ép kiểu sang kiểu con trỏ char để ghi dữ liệu nhị phân vào tệp mà không được?
1 Like