读ifstream + 写ofstream

strtok()函数详解!
1.定义
分解字符串为一组字符串。s为要分解的字符,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。在头文件#include<string.h>中。
2.原型
char *strtok(char s[], const char *delim);
3.说明
(1)当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
 

例子:

1.InfoFile.h

#pragma once

#include <list>
#include <fstream>
#include <iostream>
#include <string>
#include <afxwin.h>

#define _F_LOGIN "./login.ini"
#define _F_STOCK "./stock.txt"

using namespace std;

struct msg
{
    int id;            //商品ID
    string name;    //商品名
    int price;        //商品价格
    int num;        //商品个数
};

class CInfoFile
{
public:
    CInfoFile();
    ~CInfoFile();

    //读取登录信息
    void ReadLogin(CString &name, CString &pwd);

    //修改密码
    void WritePwd(char* name, char* pwd);

    //读取商品数据
    void ReadDocline();

    //商品写入文件
    void WriteDocline();

    //添加新商品
    void AddLine(CString name, int num, int price);

    list<msg> Is;    //储存商品容器
    int num;        //用来记录商品个数
};

 

2.InfoFile.cpp

#include "stdafx.h"
#include "InfoFile.h"

CInfoFile::CInfoFile()
{
}

CInfoFile::~CInfoFile()
{
}

//读取登录信息:从_F_LOGIN文件中读取内容,赋值给name 和 pwd变量
void CInfoFile::ReadLogin(CString & name, CString & pwd)
{
    ifstream ifs;    //创建文件输入对象
    ifs.open(_F_LOGIN);    //打开文件

    char buf[1024] = { 0 };

    ifs.getline(buf, sizeof(buf));    //读取(1024个字节)一行内容
    name = CString(buf);            //char* 转换为CString

    ifs.getline(buf, sizeof(buf));    //读取第二行内容
    pwd = CString(buf);

    ifs.close();        //关闭文件
}

//修改密码
void CInfoFile::WritePwd(char * name, char * pwd)
{
    ofstream ofs;    //创建文件输出对象
    ofs.open(_F_LOGIN);
    
    ofs << name << endl;
    ofs << pwd << endl;

    ofs.close();
}

//读取商品信息
void CInfoFile::ReadDocline()
{
    ifstream ifs(_F_STOCK);     //输入方式打开文件

    char buf[1024] = { 0 };
    num = 0;        //初始化商品数目为0
    Is.clear();
    //取出表头
    ifs.getline(buf, sizeof(buf));
    
    while (!ifs.eof())    //没到文件结尾(ifs.eof()为真--表示读取到文件结束符)
    {
        msg tmp;

        ifs.getline(buf, sizeof(buf));
        num++;        //商品数目自增

        //获取第一个字符串
        char* sst = strtok(buf, "|");    //以“|”切割
        if (sst != NULL)
        {
            tmp.id = atoi(sst);  //赋值给:商品ID
        }
        else
        {
            break;
        }

        //继续获取其他字符串
        sst = strtok(NULL, "|");
        tmp.name = sst;  //赋值给:商品名称

        sst = strtok(NULL, "|");
        tmp.price = atoi(sst); //赋值给:商品价格

        sst = strtok(NULL, "|");
        tmp.num = atoi(sst); //赋值给:商品剩余库存个数

        Is.push_back(tmp);        //把msg类型的tmp对象,放在链表后面
    }

    ifs.close();
}

//商品写入文件
void CInfoFile::WriteDocline()
{
    ofstream ofs(_F_STOCK);     //输出方式打开文件

    if (Is.size() > 0)    //商品链表有内容才执行
    {
        ofs << "商品id|商品名称|商品价格|库存" << endl;        //写入表头

        //通过循环取出链表内容,写入文件,以“|“ 分隔,结尾加换行
        for (list<msg>::iterator it = Is.begin(); it != Is.end(); it++)
        {
            ofs << it->id << "|";
            ofs << it->name << "|";
            ofs << it->price << "|";
            ofs << it->num << endl;
        }
    }

    ofs.close();
}

//添加新商品
void CInfoFile::AddLine(CString name, int num, int price)
{
    msg tmp;
    if (Is.size() > 0)
    {
        if (!name.IsEmpty() && num > 0 && price > 0)
        {
            tmp.id = Is.size() + 1;    //id自动加1
            CStringA str;
            str = name;    //CString 转CStringA
            tmp.name = str.GetBuffer(); //CStringA转char*    商品名称
            tmp.num = num;    //库存
            tmp.price = price;
            
            Is.push_back(tmp);    //放在链表后面
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值