//写数据到文件 ofstream fout("data.txt",ios::binary); if (!fout) { cout<<"can not open file to write"<<endl; } for (int i=0;i<n;i++) { fout.write(reinterpret_cast<const char*>(&a[i]),sizeof(int)); } fout.close(); //从文件中读取前100个数据并显示 ifstream fin; fin.open("data.txt",ios::binary); for (int i=0,num=0;i<100;i++) { fin.read(reinterpret_cast<char*>(&num),sizeof(int)); if (fin.eof()) { break; } cout<<num<<" "; } fin.close();