Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lab5 / Lab5 / Lab5

.cpp
Скачиваний:
1
Добавлен:
10.04.2019
Размер:
4.86 Кб
Скачать
яю// Lab5.cpp: >?@545;O5B B>G:C 2E>40 4;O :>=A>;L=>3> ?@8;>65=8O.

//



#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <time.h>

#include <conio.h>



using namespace std;



class bin_stream : public fstream

{

public:

	bin_stream(const char *fn) :

		fstream(fn, ios::out | ios::in | ios::binary) {}

	void doneOurDate(const void*, int, int);

	bin_stream &operator<<(int d)

	{

		doneOurDate(&d, sizeof(d), 0);

		return *this;

	}

	bin_stream &operator>>(int &d)

	{

		doneOurDate(&d, sizeof(d), 1);

		return *this;

	}

};



class bin_outstream : public ofstream

{

public:

	bin_outstream(const char *fn) :

		ofstream(fn, ios::out | ios::binary) {}

	void writeOurDate(const void*, int);

	ofstream &operator<<(int d)

	{

		writeOurDate(&d, sizeof(d));

		return *this;

	}

};



void bin_stream::doneOurDate(const void *Ptr, int len, int sign)

{

	if (!Ptr)   return;

	if (len <= 0)   return;

	if (sign == 0) write((char*)Ptr, len);

	else  read((char*)Ptr, len);

}





void bin_outstream::writeOurDate(const void *Ptr, int len)

{

	if (!Ptr) return;

	if (len <= 0) return;

	write((char*)Ptr, len);

}



int _tmain(int argc, _TCHAR* argv[])

{

	int i, d, m = 0, j = 0, mass[20];

	time_t t;

	srand((int)time(&t));

	bin_outstream bin_out("Bin.dat"); // A>740=85 D09;0

	if (!bin_out)

	{

		cerr << "Unable to write to Bin.dat" << endl;

		exit(1);

	}

	for (i = 0; i < 10; i++)

	{

		d = rand() % 100;

		bin_out << d;

		if (d % 2 == 1)

		{

			m++;

			mass[j] = d;

			j++;

		}

		cout << d << ' ';

	}

	cout << endl;

	bin_out.close();



	bin_outstream bin1_out("Bin1.dat"); // A>740=85 D09;0

	if (!bin1_out)

	{

		cerr << "Unable to write to Bin.dat" << endl;

		exit(1);

	}

	for (i = 0; i < 10 + m; i++)

	{

		bin1_out << 0;

	}

	bin1_out.close();



	bin_stream bin("Bin.dat"); // >1@01>B:0 D09;0

	if (!bin)

	{

		cerr << "Unable to write to Bin.dat" << endl;

		exit(1);

	}

	bin.seekp(0);

	for (i = j; i < 10 + m; i++)

	{

		bin >> mass[i];

	}

	bin.close();

	

	bin_stream bin1("Bin1.dat");

	if (!bin1)

	{

		cerr << "Unable to write to Bin1.dat" << endl;

		exit(1);

	}



	bin1.seekp(0);

	for (i = 0; i < 10 + m; i++)

	{

		bin1 << mass[i];

	}

	bin1.seekp(0);

	for (i = 0; i < 10 + m; i++)

	{

		bin1 >> d;

		cout << d << ' ';

	}

	bin1.close();

	remove("Bin.dat");

	rename("Bin1.dat", "Bin.dat");

	_getch();



    return 0;

}



Соседние файлы в папке Lab5
  • #
    10.04.201968 б1Bin.dat
  • #
    10.04.20194.86 Кб1Lab5.cpp
  • #
    10.04.20198.21 Кб1Lab5.vcxproj
  • #
    10.04.20191.38 Кб1Lab5.vcxproj.filters
  • #
    10.04.2019165 б1Lab5.vcxproj.user
  • #
    10.04.2019756 б1stdafx.cpp
  • #
    10.04.2019728 б1stdafx.h