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
    评论
"cannot execute binary file" 错误通常发生在尝试执行一个不兼容的二进制文件时。这可能是因为你正在尝试运行一个不同体系结构的可执行文件,或者文件本身可能已经损坏。[1] 解决此问题的方法之一是检查你正在尝试运行的二进制文件是否与你的系统体系结构兼容。如果不兼容,你可能需要寻找适合你系统的正确版本。 另一个可能的原因是二进制文件本身可能已经损坏。在这种情况下,你可以尝试重新下载或获取该文件的正确版本。 如果你遇到这个问题是在进行二进制文件格式分析时,可以使用工具如Hexinator来帮助你分析二进制文件格式。 如果问题是在编译时遇到的,这可能是因为你在使用不匹配的架构编译器。例如,在使用x86架构的编译器编译ARM架构的程序时会出现此错误。解决方法是重新安装与目标架构匹配的编译器或交叉编译工具链[3]。 总之,"cannot execute binary file" 错误通常是由于尝试运行不兼容的二进制文件或文件损坏引起的,解决方法包括获取正确的文件版本、检查系统架构兼容性,并使用适当的工具来分析或编译二进制文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [解决 cannot execute binary file: Exec format error](https://blog.csdn.net/qq_56992543/article/details/131897697)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [How to Approach Binary File Format Analysis](https://download.csdn.net/download/w7513118/10627335)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值