zoukankan      html  css  js  c++  java
  • const char *初值赋值以及文件读取

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cstring>
    using namespace std;
    class Student
    {
    public:
        Student(string number="123", double score=0, const char *name="noname")
        {
            this->number = number;
            this->score = score;
            this->name = new char[strlen(name) + 1];//之前写的程序总是忘记开辟空间这一步 
            strcpy(this->name, name);
        }
        ~Student() {
            //cout<<"~Stu()...."<<endl;
            delete[] name;
        }
            friend istream &operator>>(istream & is, Student& dt) {
            is >> dt.number>>dt.name>>dt.score;
            return is;
        }
        friend ostream &operator<<(ostream & os, Student& dt) {
            
            os << dt.name << "	" << dt.number << "	" << dt.score << "
    ";
            return os;
        }
    private:
        char *name;
        string number;
        double score;
    };
    
    int main()  
    {  
        Student st[5];  
        int i;  
        for(i=0;i<5;i++)  
        cin>>st[i];  
        ofstream fout("date.dat",ios::binary);  
          
        for(i=0;i<5;i++)  
        fout.write((char*)&st[i],sizeof(st[i]));  
        fout.close( );  
      
        ifstream fin("date.dat",ios::binary);  
        for(i=0;i<5;i++)  
        fin.read((char*)&st[i],sizeof(st[i]));  
         cout <<"姓名	"<<"学号	"<<"分数
    "; 
        for(i=0;i<5;i++)  
        cout <<st[i];  
        fin.close( );  
        getchar();getchar();
      
        return 0;  
    }  
    不一样的烟火
  • 相关阅读:
    前端常见跨域解决方案(全)
    小程序动画wx.createAnimation
    判断对象是否有某个属性
    占位符
    vue起航——搭建脚手架
    微信小程序-携带参数转发分享页面
    JS获取地址栏参数
    微信小程序wx:if vs hidden
    小程序websocket(心跳连接)
    初识websocket
  • 原文地址:https://www.cnblogs.com/cstdio1/p/10982673.html
Copyright © 2011-2022 走看看