MFC文件读写功能代码

.cpp

#include “stdafx.h”
#include “InfoFile.h”

CInfoFile::CInfoFile()
{
}

CInfoFile::~CInfoFile()
{
}

//读取备忘录信息
void CInfoFile::ReadDocline()
{
ifstream ifs(_F_STOCK); //定义输入流对象,输入方式打开文件
char buf[1024] = {0};
ls.clear();
//取出表头
ifs.getline(buf, sizeof(buf));
while(!ifs.eof()) //没到文件结尾
{
msg tmp;
ifs.getline(buf, sizeof(buf)); //读取一行到buf
char *sst = strtok(buf, “|”); //使用|为分隔符
if(sst !=NULL)
{
tmp.time = sst; //时间
}
else
{
break;
}
sst = strtok(NULL, “|”);
tmp.type = sst; //类型
sst = strtok(NULL, “|”);
tmp.remind = sst; //是否提醒
sst = strtok(NULL, “|”);
tmp.clock = sst; //有无闹铃
sst = strtok(NULL, “|”);
tmp.content = sst; //内容
sst = strtok(NULL, “|”);
tmp.creTime = sst; //创建时间
sst = strtok(NULL, “|”);
tmp.cType = sst; //内容类型
sst = strtok(NULL, “|”);
tmp.alarm = sst; //闹铃名
ls.push_back(tmp); //放在链表的后面
}
ifs.close(); //关闭文件
}

//备忘录写入文件
void CInfoFile::WriteDocline()
{
ofstream ofs(_F_STOCK); //输出方式打开文件
string bt = “闹钟时间|类型|提醒|闹铃|内容|创建时间”;

if(ls.size()>=0)	//商品链表有内容才执行
{
	ofs<< bt << endl;	//写入表头

	//通过迭代器取出链表内容,写入文件,以"|"分割,结尾加换行
	for (list<msg>::iterator it = ls.begin(); it!=ls.end(); it++)
	{
		ofs << it->time<<"|";
		ofs << it->type <<"|";
		ofs << it->remind <<"|";
		ofs << it->clock <<"|";
		ofs << it->content <<"|";
		ofs << it->creTime <<"|";
		ofs << it->cType <<"|";
		ofs << it->alarm <<endl;
	}
}
ofs.close();

}

void CInfoFile::DelectDocline(int k)
{
ofstream ofs(_F_STOCK); //输出方式打开文件
string bt = “闹钟时间|类型|提醒|闹铃|内容|创建时间|内容类型|铃声”;

int i=0;

if(ls.size()>=0)	//商品链表有内容才执行
{
	ofs<< bt << endl;	//写入表头

	//通过迭代器取出链表内容,写入文件,以"|"分割,结尾加换行
	for (list<msg>::iterator it = ls.begin(); it!=ls.end(); it++)
	{
		if(i == k)
		{
			i++;
			continue;
		}
		ofs << it->time<<"|";
		ofs << it->type <<"|";
		ofs << it->remind <<"|";
		ofs << it->clock <<"|";
		ofs << it->content <<"|";
		ofs << it->creTime <<"|";
		ofs << it->cType <<"|";
		ofs << it->alarm <<endl;

		i++;
	}
}
ofs.close();

}

void CInfoFile::Clear()
{
ofstream ofs(_F_STOCK); //输出方式打开文件
string bt = “闹钟时间|类型|提醒|闹铃|内容|创建时间|内容类型|铃声”;

int i=0;

if(ls.size()>=0)	//商品链表有内容才执行
{
	ofs<< bt << endl;	//写入表头

	//通过迭代器取出链表内容,写入文件,以"|"分割,结尾加换行
	for (list<msg>::iterator it = ls.begin(); it!=ls.end(); it++)
	{
		if(i == 0)
		{
			continue;
		}
	}
}
ofs.close();

}

//插入新记录
void CInfoFile::Addline( CString time ,CString type, CString remain ,CString clock ,CString content ,CString creTime, CString cType, CString alarm)
{
msg tmp;

if (ls.size() >=0)
{
	//商品名称,库存,价格有效
	if(!time.IsEmpty() && !type.IsEmpty() && !remain.IsEmpty() && !clock.IsEmpty() && !content.IsEmpty())
	{

		content.Replace(_T("\r\n"),_T("0000")); //将回车换行符替换成0000

		CStringA strTime,strType,strRemain,strClock,strContent,strCreTime,strCType,strAlarm;
		strTime = time;
		strType = type;
		strRemain = remain;
		strClock = clock;
		strContent = content;
		strCreTime = creTime;
		strCType = cType;
		strAlarm = alarm;
		tmp.time = strTime.GetBuffer();
		tmp.type = strType.GetBuffer();
		tmp.remind = strRemain.GetBuffer();
		tmp.clock = strClock.GetBuffer();
		tmp.content = strContent.GetBuffer();
		tmp.creTime = strCreTime.GetBuffer();
		tmp.cType = strCType.GetBuffer();
		tmp.alarm = strAlarm.GetBuffer();

		ls.push_back(tmp);
	}
}

}

.h

#pragma once

#include
#include
#include
#include

#define _F_STOCK “./Storage Card/memo_3/info.txt”

using namespace std;

struct msg
{
string time;
string type;
string remind;
string clock;
string content;

//不显示控件
string creTime;		 //创建时间				
string cType;           //记事类型
string alarm;           //铃声名称

};

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

//读取文件中备忘录信息
void ReadDocline();

//备忘录写入文件
void WriteDocline();

//删除行
void DelectDocline(int k);

//清空文件
void Clear();

//插入新备忘录
void Addline(CString time ,CString type, CString remain ,CString clock ,CString content ,CString creTime, CString cType, CString alarm);

list<msg> ls;	//存储备忘录记录容器
int num;			//用来记录文件中备忘录记录的个数

};

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值