C++二进制写入和读入怎么实现呢?
#include<iostream>#include<string>#include<fstream>usingnamespacestd;constintn=3;clas...
#include<iostream>#include<string>#include<fstream>using namespace std;const int n = 3;class Patient{public: int m_Id; string m_Name; double* m_temperature;//指向包含早中晚3个体温值的堆区数组public: Patient(int = 0, string = "#", double* t = NULL); Patient(const Patient& p); ~Patient(); //此处添加友元函数4 friend ostream& operator<<(ostream& out, Patient& p);};Patient::Patient(int Id, string Name, double* temperature)//此处添加构造函数,析构函数定义2{ m_Id = Id; m_Name = Name; if (temperature != NULL) { this->m_temperature = new double[3]; for (int i = 0; i < 3; i++) this->m_temperature[i] = temperature[i]; }}Patient::Patient(const Patient& p)//此处添加深复制构造函数3{ m_Id = p.m_Id; m_Name = p.m_Name; if (p.m_temperature != NULL) { this->m_temperature = new double[3]; for (int i = 0; i < 3; i++) this->m_temperature[i] = p.m_temperature[i]; } ofstream ofs("F:\\data.dat", ios::out | ios::binary); ofs.write((const char*)&p, sizeof(p)); ofs.close();}Patient::~Patient(){ if (m_temperature != NULL) { delete[] m_temperature; }}//友元函数ostream& operator<<(ostream& out, Patient& p){ out << "Id:" << p.m_Id << " 名字:" << p.m_Name << " " << endl; return out;}int main(){ double temp[n] = { 37.5, 38, 37.8 }; int id = 0; Patient pat1(1, "Rose", temp), pat2(2, "Tom"), pat3(pat1); cout << pat1 << pat2 << pat3; //读出二进制文件中的内容并输出到显示器6 ifstream ifs("F:\\person.txt", ios::in | ios::binary); ifs.read((char*)&pat3, sizeof(pat3)); id = pat3.m_Id; cout << "Id :" << id; cout << "温度 : " << temp[0] << "," << temp[1] << "," << temp[2] << endl; system("pause"); return 0;}
展开
1个回答
展开全部
#include <fstream>
写二进制文件应该使用ofstream类,文件的打开du模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。
下面是示例代码,用于写入文件。
std::ofstream fout("a.dat", std::ios::binary);
int nNum = 20;
std::string str("Hello, world");
fout.write((char*)&nNum, sizeof(int));
fout.write(str.c_str(), sizeof(char) * (str.size()));
fout.close();
而写文本文件则比较简单,如下:
std::ofstream fout("b.dat");
int nNum = 20;
std::string str("Hello, world");
fout << nNum <<","<< str << std::endl;
fout.close();
读二进制文件
读取二进制文件可以使用ifstream 类来进行,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。
下面是示例代码:
std::ifstream fin("a.dat", std::ios::binary);
int nNum;
char szBuf[256] = {0};
fin.read((char*)&nNum, sizeof(int));
fin.read(szBuf, sizeof(char) * 256);
std::cout <<"int = "<< nNum << std::endl;
std::cout <<"str = "<< szBuf << std::endl;
fin.close();
写二进制文件应该使用ofstream类,文件的打开du模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。
下面是示例代码,用于写入文件。
std::ofstream fout("a.dat", std::ios::binary);
int nNum = 20;
std::string str("Hello, world");
fout.write((char*)&nNum, sizeof(int));
fout.write(str.c_str(), sizeof(char) * (str.size()));
fout.close();
而写文本文件则比较简单,如下:
std::ofstream fout("b.dat");
int nNum = 20;
std::string str("Hello, world");
fout << nNum <<","<< str << std::endl;
fout.close();
读二进制文件
读取二进制文件可以使用ifstream 类来进行,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。
下面是示例代码:
std::ifstream fin("a.dat", std::ios::binary);
int nNum;
char szBuf[256] = {0};
fin.read((char*)&nNum, sizeof(int));
fin.read(szBuf, sizeof(char) * 256);
std::cout <<"int = "<< nNum << std::endl;
std::cout <<"str = "<< szBuf << std::endl;
fin.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询