写二进制文件
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class File
{
private:
ofstream f;
string s;
public:
File()
{
f.open("002.dat",ios::out);
}
void Write()
{
s="abcde";
f.write((char*)s.c_str(),s.size());//二进制文件每次要指定读写的字节数
}
~File()
{
f.close();
}
};
int main()
{
File f;
f.Write();
return 0;
}
注意读二进制文件时,在程序文件目录下一定要有该文件
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class File
{
private:
ofstream f1;
ifstream f2;
string s;
public:
File()
{
f1.open("002.dat",ios::binary);
f2.open("003.dat",ios::binary);
}
~File()
{
f1.clo