以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  期货人生  (http://weistock.com/bbs/list.asp?boardid=7)
----  [原创]person  (http://weistock.com/bbs/dispbbs.asp?boardid=7&id=66164)

--  作者:z7c9
--  发布时间:2014/6/15 15:24:26
--  [原创]person
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct Person{
    string name;
    int age;
    double salary;
};

ofstream& operator<<(ofstream &fout,const Person &p){
    fout << p.name << "\\n"  << p.age <<"\\n" << p.salary << "\\n";
    return fout;
}

ifstream& operator>>(ifstream &fin,Person &p){
    fin >> p.name >> p.age >> p.salary;
    return fin;
}

void writeData(){
    ofstream fout("d:/test.b");

    for(int i = 0;i < 20; ++i){
        Person p;
        p.name = "superman";
        p.age = i * 2;
        p.salary = i * 3.0;
        fout << p;
    }

    fout.close();
}

void readData(){
    ifstream fin("d:/test.b");

    Person p;

    while(fin >> p){
        cout << p.name << ","<< p.age << "," << p.salary << endl;
    }

    fin.close();
}

int main(int argc, char *argv[])
{
    writeData();
    readData();

    system("pause");
}
	


--  作者:z7c9
--  发布时间:2014/6/15 16:29:06
--  
pic
图片点击可在新窗口打开查看此主题相关图片如下:snap4.png
图片点击可在新窗口打开查看

--  作者:z7c9
--  发布时间:2014/6/15 22:52:22
--  
pic
图片点击可在新窗口打开查看此主题相关图片如下:snap6.png
图片点击可在新窗口打开查看
[此贴子已经被作者于2014/6/15 22:53:39编辑过]

--  作者:z7c9
--  发布时间:2014/6/23 9:04:12
--  
portfo
图片点击可在新窗口打开查看此主题相关图片如下:qq截图20140623090237.png
图片点击可在新窗口打开查看
lio
[此贴子已经被作者于2014/6/23 9:05:41编辑过]