vector与结构体联合使用 在磁盘中生成.txt 文件

一下纯属个人总结、欢迎拍砖!谢谢

我意思到以练促进学习C++编程基础是很有帮助的

这篇文章是我为了熟悉掌握文件流和STL中的vector以及结构体三个只知识点所写的代码:

#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct  cashier
{
    string cashierNumber;
    string password;
};

cashier _cashier;
vector <cashier> account;

void main()
{
    ofstream root_file;
    root_file.open("d:\\rootNumber.txt ",ios::out);
    if (!root_file)
    {
        cout<< "创建根账户失败!"<<endl;
    }

    _cashier.cashierNumber="888888";
    _cashier.password="888888";
    account.push_back(_cashier);
    for (vector<cashier>::iterator it=account.begin();
        it!=account.end();it++)
    {
        root_file<<it->cashierNumber<<" ";
        root_file<<it->password;
    }
    root_file.close();
    system("pause");
}

在磁盘中生成的文件如下图所示:

 

posted @ 2013-04-30 04:48  hww836967373  阅读(271)  评论(0编辑  收藏  举报