01/10/2018, 00:33

[Hỏi] 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 ạ?

#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();
}
Nguyễn Xuân Phúc viết 02:46 ngày 01/10/2018
Làm sao để có thể hiển thị syntax highlighting bằng markdown? Các bạn phải đánh dấu ``` như ví dụ dưới đây Chú ý, dấu ``` được tạo ra bởi nút nằm bên trái số 1 trên bàn phím, nút này sẽ là ~ khi bấm giữ Shift Ví dụ cho C Nội dung: ``` void main() { } ``` Và đừng quên ``` ở cuối Kết quả void main() { } Ví dụ cho Pascal Nội dung: ``` Program HelloWorld; Begin WriteLn('Hello world!') {no ";" is required after the last statement of a block - adding one adds a "null stateme…
Bài liên quan
0