二进制流操作

时间:2022-09-24 14:57:46

#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
void main()
{
  ofstream f1("wr1.dat");
  if(!f1)
  {
    cerr<<"wr1.dat file not open!"<<endl;
    exit(1);
  }
  //f1.seekp(beg);
  for(int i=0;i<21;i++)
    f1<<i<<" ";
  f1.close();
  ifstream f2("wr1.dat",ios::in|ios::nocreate);
  if(!f1)
  {
    cerr<<"wr1.dat file not open!"<<endl;
    exit(1);
  }
  int x;
  while(f2>>x)
    cout<<x<<' ';
  cout<<endl;
  f2.close();
}