- •Практичне заняття 8 Теоретична частина
- •Завдання
- •Int main()
- •Void push(int var)
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 9 Теоретична частина
- •Зміст завдання
- •Лабораторне заняття 10 Теоретична частина
- •Практичне заняття 9 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Практичне заняття 10-11 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 11-12 Теоретична частина
- •Завдання 1
- •Завдання 2
- •Завдання 3
- •Int main()
- •Практичне заняття 12-13 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 13 Теоретична частина
- •Завдання 1
- •Int main()
- •Завдання 2
- •Практичне заняття 14 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Лабораторне заняття 14 Теоретична частина
- •Завдання
Int main()
{person pers;
pers.getData();
ofstream outfile("person.dat",ios::binary);//створити обєкт ofstream
outfile.write(reinterpret_cast<char*>(&pers),sizeof(pers));
getch();
return 0;
}
Програма 26.7
#include <fstream>
#include<iostream>
#include<conio>
using namespace std;
///////////
class person
{protected:
char name[80];
short age;
public:
void showData()
{ cout<<"Imia: "<<name<<endl;
cout <<"Vik: "<<age<<endl;
}
};
/////////////////
Int main()
{person pers;
ifstream infile("person.dat",ios::binary);//створити потік
infile.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showData();
getch();
return 0;
}
Програма 26.7а
#include<fstream>
#include <iostream>
#include<conio>
using namespace std;
class person
{protected:
char name[80];
int age;
public:
void getData()
{ cout<<"Vvedit imia: ";cin>>name;
cout <<"Vvedit vik: ";cin>>age;
}
void showData()
{ cout<<"Imia: "<<name<<endl;
cout <<"Vik: "<<age<<endl;
}
};
///////////////////
Int main()
{char ch;
person pers;
fstream file;
//відкрити до дозапису
file.open("group.dat",ios::app|ios::out|
ios::in|ios::binary);
do
{cout<<"\nVvedit dani pro ludynu: \n";
pers.getData();
file.write(reinterpret_cast<char*>(&pers),sizeof(pers));
cout<<"Continue (y/n)?";
cin>>ch;
} while(ch=='y');
file.seekg(0);//поставити вказівник на початок файлу
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
while(!file.eof())
{cout<<"\nPersona: ";
pers.showData();
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
}
cout<<endl;
getch();
return 0;
}
Програма 26.8
#include<fstream>
#include<iostream>
#include<conio>
using namespace std;
class person
{ protected:
char name[80];
int age;
public:
void getData()
{cout<<"\nVvedit ima: ";cin>>name;
cout<<"Vvedit vik: ";cin>>age;
}
void showData()
{cout<<"\n Ima="<<name;
cout<<"\n Vik="<<age;
}
};
//////////////////////
Int main()
{
person pers;
ifstream infile;
infile.open("group.dat",ios::in|ios::binary);
infile.seekg(0,ios::end);
int endposition=infile.tellg();
int n=endposition/sizeof(person);
cout<<"\nV faili "<<n<<" 4olovik ";
cout<<"\nVvedit N persony: ";
cin>>n;
int position=(n-1)*sizeof(person);
infile.seekg(position);
infile.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showData();
cout<<endl;
getch();
return 0;
}
Програма 26.9
#include<fstream>
#include<iostream>
#include<conio>
using namespace std;
#include<process.h>
const int MAX=1000;
int buff[MAX];
//////////////
Int main()
{int j=0;
for(j=0;j<MAX;j++) //заповнити буфер даними
buff[j]=j;
ofstream os; //створити вхідний потік
os.open(“edata.dat”,ios::trunc|ios::binary);
if(!os)
{cerr<<”Nemozlyvo vidkryty vhidnyj file\n”;getch();exit(1);}
cout<<”Write…\n”;
//записати вміст буферу
os.write(reinterpret_cast<char*>(buff),MAX*sizeof(int));
if(!os)
{cerr<<”Zapys v file nemozlyvyj\n”;getch();exit(1);}
os.close(); //закриття потоку
for(j=0;j<MAX;j++)
buff[j]=0; //очистка буфера
ifstream is;
is.open(“udata.dat”,ios::binary); //імена edat.dat i udata.dat відрізняються
if(!is)
{cerr<<”Nemozlyvo vidkryty vhidnyj file\n”;getch();exit(1);}
cout<<”Ide 4ytanna…\n”;//читання файлу
is.read(reinterpret_cast<char*>(buff),MAX*sizeof(int));
if(!is)
{cerr<<”Nemozlyvo 4ytaty file\n”;getch();exit(1);}
for(j=0;j<MAX;j++)
if(buff[j]!=j)
{cerr<<”\nDani nekorektni\n”;getch();exit(1);}
cout<<”\nDani korektni\n”;
getch();
return 0;
}
Програма 26.10
#include<fstream>
#include<iostream>
#include<conio>
using namespace std;
