Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 6.3.4..правильный.cpp :6.3. БИНАРНЫЙ ФАЙЛ СТРУКТУР.Написать программу для работы с базой данных, содержащей записи со сведениями о студентах : ФИО, возраст, пол, курс, успеваемость, в которой должны выполняться следующие действия :создание файла/просмотр файла/выполнение задания 6.2.
- // Задача: создание бинарного файла случайных целых чисел,
- // вывод чисел на экран, создание нового файла только из
- // четных чисел исходного файла
- #include "stdafx.h"
- #include <ctime>
- #include <stdlib.h>
- #include <fstream>
- #include <string.h>
- #include <iostream>
- using namespace std;
- //
- const int m = 40;
- struct student // объявление структуры student
- {
- char name[m];
- int kyrs;
- int vozrast;
- bool pol;
- double yspevaemost;
- };
- student read(); // ввод значений полей структуры
- int yslovie(char *stroka, int k);
- void vyvod(char *stroka);
- void vvod(int n, char *stroka);
- void print(student stud);
- int main()
- {
- int n;
- const int Max = 80;
- char stroka[Max];
- cout << "Enter Kolvo man = " << endl;
- cin >> n;
- strcpy_s(stroka,"file.bin");
- vvod(n,stroka);
- vyvod(stroka);
- int k;
- cout << "Enter Kyrs = " << endl;
- cin >> k;
- int kolvo=yslovie(stroka, k);
- cout << "Kolvo students pol=m,kyrs-k = " << kolvo << endl;
- return 0;
- }
- //
- student read() // ввод значений полей структуры
- {
- student stud;
- cin.get();
- cout << " FIO = ";
- cin.getline(stud.name, m);
- cout << endl;
- cout << "Kyrs = ";
- cin >> stud.kyrs;
- cout << endl;
- cout << "Vozrast = ";
- cin >> stud.vozrast;
- cout << endl;
- cout << "Pol 1-M,0-W = ";
- cin >> stud.pol;
- cout << endl;
- cout << "Yspevaemost = ";
- cin >> stud.yspevaemost;
- cout << endl; // ввод значения key
- //cin.get();
- return stud;
- }
- //
- void vyvod(char *stroka)
- {
- student stud;
- ifstream filein(stroka, ios::binary);
- while (!filein.eof())
- {
- filein.read(reinterpret_cast <char*> (&stud), sizeof(student));
- print(stud);
- }
- filein.close();
- }
- void vvod(int n, char *stroka)
- {
- student stud;
- ofstream fileout(stroka, ios::binary);
- for (int i = 1;i <= n;i++)
- {
- stud = read();
- fileout.write(reinterpret_cast <char*> (&stud), sizeof(student));
- }
- fileout.close();
- }
- void print(student stud) // вывод значений полей структуры
- {
- cout << " FIO = " << stud.name;
- cout << " Kyrs = " << stud.kyrs;
- cout << " Vozrast = " << stud.vozrast;
- cout << " Pol = " << stud.pol;
- cout << " Yspevaemost = " << stud.yspevaemost << endl;
- }
- int yslovie(char *stroka, int k)
- {
- int kolvo = 0;
- student stud;
- ifstream filein(stroka, ios::binary);
- while (!filein.eof())
- {
- filein.read(reinterpret_cast <char*> (&stud), sizeof(student));
- if (stud.kyrs == k && stud.pol == 1)
- {
- kolvo++;
- }
- }
- filein.close();
- return kolvo;
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement