Binary File

Write numbers to a binary file and read them back

#include <iostream>
#include <fstream>
using namespace std;
int main(void){
   float fnum[4] = {11.22, -33.44, 55.66, 77.88};
   int i;

   ofstream out("numbers.asc", ios::out | ios::binary);
   if(!out){
      cout << "Cannot open file.";
      exit (1);
   }
   out.write((char *) &fnum, sizeof(fnum));
   out.close();
   for (i=0; i<4; i++)
      fnum[i] = 0.0;
   ifstream in("numbers.asc", ios::in | ios::binary);
   if(!in) {
      cout << "Cannot open file.";
      exit (1);
   }
   in.read((char *) &fnum, sizeof(fnum));
   cout << in.gcount() << " bytes read." << endl;
   for (i=0; i<4; i++)
      cout << fnum[i] << " ";
   in.close();
}
  
    

reading a complete binary file

#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
  ifstream file ("example.txt", ios::in|ios::binary|ios::ate);

  if (file.is_open()){
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the complete file content is in memory";

    delete[] memblock;
  }else 
    cout << "Unable to open file";
  return 0;
}
  
    

Binary Files

#include <iostream>
using namespace std;  
#include <fstream>
int main () 
{
 long start,end;

 ifstream myfile ("test.txt", ios::in|ios::binary);
 start = myfile.tellg();
 myfile.seekg (0, ios::end);
 end = myfile.tellg();
 myfile.close();
 cout << "size of " << "test.txt";
 cout << " is " << (end-start) << " bytes.\n";
 return 0;
}

seeks particular person in file

#include <fstream>
  #include <iostream>  
  using namespace std;  

  class person      
  {  
     protected:  
        char name[80];
        int age;      
     public:  
        void getData(){  
           cout << "\n   Enter name: "; cin >> name;  
           cout << "   Enter age: "; cin >> age;  
        }  
        void showData(void){  
           cout << "\n   Name: " << name;  
           cout << "\n   Age: " << 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 << "\nThere are " << n << " persons in file";  
    
     cout << "\nEnter person number: ";  
     cin >> n;  
     int position = (n-1) * sizeof(person);  
     infile.seekg(position);        
                                    
     infile.read( reinterpret_cast<char*>(&pers), sizeof(pers) );  
     pers.showData();               
     cout << endl;  
     return 0;  
     }
  

binary input and output with integers

#include <fstream>            
#include <iostream>  
using namespace std;  
const int MAX = 100;          
int buff[MAX];                
  
int main(){  
   for(int j=0; j<MAX; j++)   
      buff[j] = j;              
                              
   ofstream os("edata.dat", ios::binary);  
                              
   os.write( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  
   os.close();                
  
   for(int j=0; j<MAX; j++)       
      buff[j] = 0;  
                              
   ifstream is("edata.dat", ios::binary);  

   is.read( reinterpret_cast<char*>(buff), MAX*sizeof(int) );  

   for(int j=0; j<MAX; j++){      
      if( buff[j] != j ){ 
         cerr << "Data is incorrect\n"; return 1;
      }
   }     
   cout << "Data is correct\n";  
   return 0;  
}
  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值