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

本文通过一个具体的示例介绍了如何使用C++中的文件流(fstream)进行文件操作,并结合STL容器vector来实现数据的存储。示例中创建了一个简单的收银员账号管理系统,演示了如何将数据写入磁盘文件。

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

我意思到以练促进学习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");
}

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

 

转载于:https://www.cnblogs.com/hww836967373/archive/2013/04/30/3051537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值