Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
class stream:public fstream{
public:
stream(const char* filename):fstream(filename, ios::in | ios::out | \
ios::binary){};
void in(const void* data, int len)
{
write((char *)data, len);
}
void out(void *buffer, int size)
{
read((char *)buffer, size);
}
stream &operator << (int data)
{
in(&data, sizeof(data));
return *this;
}
stream &operator >> (int& data)
{
out(&data, sizeof(data));
return *this;
}
};
void main()
{
stream file("bin.dat");
int data;
clrscr();
cout<<"\nfirst\n";
while(!file.eof())
{
file>>data;
cout<<data<<endl;
}
file.seek(0, ios::beg);
cout<<"second\n";
while(!file.eof())
{
file>>data;
cout<<data<<endl;
}
file.close();
}
/*
int data, records, number, record_size = sizeof(int),\
minmas[100], minelements = 0;
cout<<"\nEnter Number: ";
cin>>number;
file<<1;
file<<2;
file<<3;
file<<4;
file<<5;
file<<6;
file<<7;
file<<8;
file<<9;
file<<10;
file<<10;
file.seekg(-record_size, ios::end);
file>>records;
records;
file.seekg(0, ios::beg);
for(int i = 0; i<records; i++)
{
file>>data;
cout<<"Record no "<<i<<" = "<<data<<endl;
if(data<number)
{
minmas[i] = data;
minelements++;
}
}
file.seekp(records/2, ios::beg);
for(int j = 0; j<minelements ; j++)
{
file<<minmas[j];
}
file.seekg(0, ios::beg);
while(!file.eof()){
file>>data;
cout<<"\nData: "<<data;
}
cout<<"\nData: "<<data;
}
cout<<"\nRecords Found: "<<records<<endl;
file.seekg(-record_size*records, ios::cur);
file>>data;
cout<<"\nData: "<<data;
// file.seekp();
// file>>data;
// cout<<"\nData: "<<data;
/*
while(!file.eof()){
file>>data;
cout<<"\nData: "<<data;
records++;
}
// while(!file.eof()){
// file>>data;
// cout<<"\nDEBUG:3: "<<data;
// records++;
// }
// file.seekg(0, ios::beg);
//file.seekp(records/2);
// while(records-->0)
// {
// file>>data;
// cout<<"\nDEBUG:2: "<<data;
//if(data<number)file<<data;
// }
// file.seekg(0, ios::beg);
//while(!file.eof()){
// file>>data;
// cout<<"\nData : "<<data<<endl;
// }
file.close();
}
*/