首页 > 其他分享 >5.18打卡

5.18打卡

时间:2023-05-18 22:23:11浏览次数:41  
标签:info ifstream ifs Dog configfile 打卡 5.18 dog1

一、实验内容

定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。

二、实验代码

#include<bits/stdc++.h>
using namespace std;
class Dog
{
public:
 double weight;
 int age;
 } ;
 
 struct ConfigManager
 {
  public:
   ConfigManager(const char* configfile = "bitsserver.config")
    :_configfile(configfile)
    {
    }
  void WriteBin(const Dog& info)
  {
   ofstream ofs(_configfile, ifstream::out | ifstream::binary);
   ofs.write((const char*)&info, sizeof(Dog));
   ofs.close();
  }
  void ReadBin(Dog& info)
  {
   ifstream ifs(_configfile, ifstream::in | ifstream::binary);
   ifs.read((char*)&info, sizeof(Dog));
   ifs.close();
  }
  
  void WriteText(const Dog& info)
  {
   ofstream ofs(_configfile);
   ofs << info.age << endl;
   ofs << info.weight << endl;
   ofs.close();
  }
  void ReadText(Dog& info)
  {
   ifstream ifs(_configfile);
   ifs >> info.age;
   ifs >> info.weight;
   ifs.close();
  }
 private:
  string _configfile; 
 
 };
 int main()
 {
  ConfigManager cfgMgr;
  Dog dog1;
  Dog dog2;
  dog1.weight = 5;
  dog1.age = 10;
  //文本 
  cfgMgr.WriteText(dog1);
  cfgMgr.ReadText(dog2);
  cout << dog2.age << endl;
  cout << dog2.weight << endl;
  //二进制 
  cfgMgr.WriteBin(dog1);
  cfgMgr.ReadBin(dog2);
  cout << dog2.age << endl;
  cout << dog2.weight << endl; 
  
  return 0; 
 }

三、测试截图

 

 

标签:info,ifstream,ifs,Dog,configfile,打卡,5.18,dog1
From: https://www.cnblogs.com/binglinll/p/17413457.html

相关文章

  • 5.18每日总结
    今日进行了python的学习。对于昨天的测试代码进行了分析学习。R7-1字典合并d1=eval(input())d2=eval(input())forkeyind2.keys():d1[key]=d1.get(key,0)+d2[key]t=list(d1.items())t.sort(key=lambdax:ord(x[0])iftype(x[0])==strelsex[0])......
  • 每日总结-23.5.18
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd&qu......
  • 5-18打卡
    递归写爬楼梯#include<stdio.h>//定义一个函数,用来打印每次爬的台阶数voidprint_steps(intsteps[],intn){printf("一种可能的方法是:");for(inti=0;i<n;i++){printf("%d",steps[i]);}printf("\n");}//定义一个递......
  • 每日打卡
    把真分数分解为埃及分数问题描述:古埃及人用的分数都是分子为一的分数,将真分数拆分成埃及分数问题分析:1如果分子为1直接输出2.分母是分子的倍数,化简后输出3.如果不能消去的话可以分出一个a/b+1和c出来代码:#include<stdio.h>intmain(){ longinta,b,c; printf("请输入a......
  • 5.18
    #include<iostream>usingnamespacestd;#include<string>classstudent{public:   voidshangke();protected:   stringname;   intbj;   intid;};classteacher{public:   voidjiaoke();protected:   intID;   intgz;};c......
  • 5.18总结
    packagecom.mf.jdbc.exmaple;importcom.alibaba.druid.pool.DruidDataSourceFactory;importcom.mf.jdbc.Brand;importorg.junit.Test;importjavax.sql.DataSource;importjava.io.FileInputStream;importjava.sql.Connection;importjava.sql.PreparedStatement;......
  • c++打卡练习(33)
    求一个真分数的埃及分数表示埃及分数是指只使用1作为分子的分数,例如8/11=1/2+1/5+1/55+1/110;流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){ inta,b,c,i,j; cout<<"请输入一个真分数"<<endl; cin>>a; getchar(); cin>>b; if(a>b){ cout<<......
  • 5.18CSDN贪吃蛇
    贪吃蛇 速度不要调很慢会影响判断#include<iostream>#include<windows.h>#include<conio.h>#include<deque>#include<ctime>#include<stdexcept>usingnamespacestd;structSnake{//蛇类结构体charimage;shortx,y;//坐标};classsnakeGame......
  • 第二十三天打卡
    一、问题描述C语言实现两个不同的自然数A和B,如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。求3000以内的全部亲密数。二、设计思路1、a和b都是3000以内2、穷举a在3000以内(或穷举b在3000以内)3、通过......
  • 2023.5.18
    importosimportpandasaspd#添加测试数据os.makedirs(os.path.join('.','data'),exist_ok=True)data_file=os.path.join('.','data','house_tiny.csv')withopen(data_file,'w')asf:   f.write('N......