//arb2014_binary_gus_9_2
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
char fname[20]; // //
int x;
cout << "Enter name of file:";
cin >> fname;
// .
ofstream out(fname, ios::binary);
if(!out) // //
{
cout << "Not open file"<<endl;
exit(1);
}
cout << "Enter integer numbers. Number 9999 - end of input"<<endl;
//
// .
// sizeof(int) ,
// int/
while(cin>>x && x!= 9999)
out.write((char *) &x, sizeof(int));
out.close();
// .
ifstream in(fname, ios::binary);
if(!in)
{
cout << "Not open file"<<endl; exit(1);
}
cout << "Output file:"<<endl;
// .
while(in.read((char *) &x, sizeof(int)))
cout << x << ' ';
cout << endl;
in.close();
}
3
, . , file1.bin. create().
// check().
. .
. ios::app out1_bin.open("file1.bin",ios::binary | ios::app);
// arb2013_gusen_10_3_bin
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
//////////////////////////
void create(ifstream &f, ofstream &g)
{
int x;
if(!f)
{
cout << "Can not open file file1.bin"<<endl;
exit(1);
}
while(f>>x)
{
g.write((char *) &x, sizeof(int));
}
}
/////////////////////////////////////////
void check(ifstream &f, ofstream &g)
{
int x;
if(!f)
{
cout << "Can not open file file1.bin"<<endl;
exit(1);
}
while(f.read((char*)&x, sizeof(int)))
{
g<<x<<' ';
}
g<<endl;
}
////////////////////////////////////////
void main()
{
ifstream in1("arb.txt");
ofstream out1_bin("file1.bin",ios::binary);
create(in1,out1_bin);
in1.close();
out1_bin.close();
ifstream in1_bin("file1.bin",ios::binary);
ofstream out1("out1.txt");
check(in1_bin,out1);
out1.close();
in1_bin.close();
in1.open("arb.txt");
out1_bin.open("file1.bin",ios::binary | ios::app);
|
|
//
//
// . ios::app
create(in1,out1_bin);
in1.close();
out1_bin.close();
in1_bin.open("file1.bin",ios::binary);
out1.open("out1.txt");
check(in1_bin,out1);
out1.close();
in1_bin.close();
}
10.5
/* .
.
, .
*/
// D2.
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
void main()
{
int x;
ifstream myin("file.bin", ios::binary);
if(!myin)
{
cout << "Cant open file file1.bin"<<endl;
exit(1);
}
ofstream myout1("file1.bin", ios::binary);
//
ofstream myout2("file2.bin", ios::binary);
//
while(myin.read((char *) &x, sizeof(int)))
{
if(x>0)
myout1.write((char *) &x, sizeof(int));
else
myout2.write((char *) &x, sizeof(int));
}
myout1.close();
myout2.close();
//
// .
ifstream myplus("file1.bin", ios::binary);
cout << "File of positive numbers"<<endl;
while (myplus.read((char *) &x, sizeof(int)))
cout << x << ' '; cout << endl;
ifstream myother("file2.bin", ios::binary);
cout << "File of other numbers"<<endl;
while(myother.read((char *) &x, sizeof(int)))
cout << x << ' ';
cout <<endl;
myplus.close(); myother.close();
}
10.6
. .
. .
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
const int N=5;
int x[N], sum, len, i;
ifstream myin("file.bin", ios::binary); //
if(!myin)
{
cout << "Cant open file file.bin"<<endl;
exit(1);
}
sum = 0;
while(!myin.eof())
// . ( &)
{
myin.read((char *) x, N*sizeof(int));
// gcount() .
len = myin.gcount()/sizeof(int);
for(i=0; i<len; i++) sum+=x[i];
}
myin.close();
cout << "Sum =" << sum << endl;
}
11.3
, :
- ;
- ;
- ;
- ;
- 9 : , , (-1, -0), (2,3,4,5)
( 2,3,4,5)
:
923 . 10.06.2006
11111 1 4
22222 0 2
33333 1 2
44444 1 5
55555 1 3
66666 1 3
77777 1 5
88888 1 4
99999 1 4
//arb2014_gus_11_3_december_2014
#include <fstream>
|
|
#include <iostream>
using namespace std;
void main()
{
struct student // student
{
char name[30];
char num_z[6];
int zach;
int mark_exam;
};
struct vedomost // vedomost
{
int group;
char predmet[15];
char date[10];
char examenater[15];
student spisok[9]; // student -
// vedomost
};
vedomost subject1; // vedomost
int i;
int marks[4]; // C
ifstream in("vedomost.txt");
if(!in)
{
cout << "Can't open file vedomost.txt"<<endl;
exit(1);
}
//
in >> subject1.group >> subject1.predmet >> subject1.date >> subject1.examenater;
for(i=0; i<9; i++)
in >> subject1.spisok[i].name >> subject1.spisok[i].num_z
>> subject1.spisok[i].zach >> subject1.spisok[i].mark_exam;
//
for(i=0; i<4; i++)
marks[i] = 0;
//
for(i=0; i<9; i++)
switch (subject1.spisok[i].mark_exam)
{ case 2:
marks[0]++;
break;
case 3:
marks[1]++;
break;
case 4:
marks[2]++;
break;
case 5:
marks[3]++;
break;
}
ofstream out("out.txt");
out<<" the final performance in the group"<<endl;
out << "marks 5 " << marks[3] <<endl;
out<<"marks 4 " << marks[2]<<endl;
out<< "marks 3 " << marks[1]<<endl;
out<<"marks 2 " << marks[0] << endl;
out.close();
}
out.txt
( )
the final performance in the group
marks 5 2
marks 4 3
marks 3 2
marks 2 2
11.4
- . .
, :
- ;
- ;
- (-1 -0);
- , , .
1.
2. , . ( .)
:
923 1
924 0 19
924 1 .
922 1
924 0 17
924 0 25
//arb2014_gus_11_4_december_2014
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
struct student
{
char name[30];
int group;
int army;
union // Pascal
{
char voin_zv[15];
int age;
};
};
student man;
ifstream in("student.txt");
if(!in)
{
cout << "Can't open file student.txt"<<endl;
exit(1);
}
//
ofstream binout("voenkomat.bin",ios::binary);
while(!in.eof())
{
in >> man.name >> man.group >> man.army;
if (man.army)
in >> man.voin_zv;
else
in >> man.age;
if(!in.eof())
binout.write((char *) &man,sizeof(struct student));
}
binout.close();
//
ifstream binin("voenkomat.bin",ios::binary);
ofstream out("result.txt"); // ()
//
out << " "<<endl<<endl;
//
while(1)
{
binin.read((char *) &man,sizeof(struct student));
if (binin.eof())
break;
if(!man.army)
{
/* .
width(N) N.
fill(C) . - ( ).
setf(ios::left) ( ).
|
|
unsetf(ios::left) . */
out.width(29);
out.fill('-');
out << '-' <<endl;
out.fill(' ');
out.width(1);
out << '|';
out.width(15);
out.setf(ios::left);
out << man.name;
out.width(1);
out << '|';
out.width(5);
out.unsetf(ios::left);
out << man.group;
out.width(1);
out << '|';
out.width(5);
out << man.age;
out.width(1);
out << '|' <<endl;
out.width(29);
out.fill('-');
out << '-' << endl;
out.fill(' ');
}
}
}
result.txt
-----------------------------
| | 924| 19|
-----------------------------
-----------------------------
| | 924| 17|
-----------------------------
-----------------------------
| | 924| 25|
Real salary
1wwwwwwwwwwww 120 22 33 44 44 55 66 77 88 99 2 2 0.15
12
salary
:
-
-
-
-
*0.87*(1-credit)/12;
.
.
. .
.
5 .
//arb2014_binary_real_salary
#include <iostream>
#include <fstream>
using namespace std;
const int n=12;
struct salary
{
char fam[30];
int m[n];
double credit;
double real_salary;
};
//////////////////////////////
void main()
{
int i,j, num=0;
salary* z;
ifstream in("salary.txt");
ofstream out("salary.bin", ios::binary);
salary x;
if(!in)
{
cout<<"file not open!";
exit(-1);
}
while (in)
{
in>>x.fam;
for (i=0; i<n; i++)
{
in>>x.m[i];
}
in>>x.credit;
double s=0;
for(i=0; i<n; i++)
{
s=s+x.m[i];
}
s=s*0.87*(1-x.credit);
x.real_salary=s/12;
if(in)
{
out.write ((char*)&x, sizeof (salary));
num++;
}
}
in.close();
out.close();
z=new salary[num];
ifstream in1("salary.bin", ios::binary);
i=0;
while (i<num && in1.read((char*)&z[i], sizeof(salary)))
{
i++;
}
in1.close();
for(i=0; i<num-1; i++)
for(j=i+1; j<num; j++)
{
if(z[i].real_salary<z[j].real_salary)
{
x=z[i];
z[i]=z[j];
z[j]=x;
}
}
cout<<" high salary 5 employees"<<endl;
for(i=0; i<5; i++)
cout<<z[i].fam<<" "<<z[i].real_salary<<endl;
}
salary.txt
1wwwwwwwwwwww 120 22 33 44 44 55 66 77 88 99 2 2 0.15
2wwwwwwwwwwww 135 22 33 44 44 55 66 77 88 99 2 2 0.15
3wwwwwwwwwwww 126 22 33 44 44 55 66 77 88 99 2 2 0.15
4wwwwwwwwwwww 12 226 33 44 44 55 66 77 88 99 2 2 0.15
5wwwwwwwwwwww 126 225 33 44 44 55 66 77 88 99 2 2 0.15
6wwwwwwwwwwww 12 22 33 44 44 55 66 77 88 99 2 2 0.15
7wwwwwwwwwwww 12 22 336 44 44 55 66 77 88 99 2 2 0.15
8wwwwwwwwwwww 12 22 336 44 44 55 66 77 88 99 2 2 0.15
9wwwwwwwwwwww 12 22 33 446 44 55 66 77 88 99 2 2 0.15
10wwwwwwwwwwww 12 22 33 446 44 55 66 77 88 99 2 2 0.15
12wwwwwwwwwwww 12 22 336 44 44 55 66 77 88 99 2 2 0.15
|
|
13wwwwwwwwwwww 12 226 33 44 44 55 66 77 88 99 2 2 0.15
14wwwwwwwwwwww 126 22 33 44 44 55 66 77 88 99 2 2 0.15
15wwwwwwwwwwww 126 22 33 44 44 55 66 77 88 99 2 2 0.15
16wwwwwwwwwwww 126 22 336 644 44 55 66 77 88 99 2 2 0.15
17wwwwwwwwwwww 12 22 336 44 44 55 66 77 88 99 2 2 0.15