C++文件数据写入和读取

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++文件数据写入和读取

想吃读研的苦   2022-06-20 我要评论

一:没有数据,准备数据,写入文件

1.main.cpp

#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
#include"CData.h"
#include"CStaff.h"
 
int main()
{
	CData::userInit();//数据初始化
	return 0;
}

2.CStaff.h

#ifndef CSTAFF_H
#define CSTAFF_H
#define ADMIN 1
#define MANAGER 2
#define WAITER 3
#include<string>
#include<iostream>
using namespace std;
 
class Staff
{
public:
	Staff();
	Staff(int id,string name,string pwd,int prole);
	~Staff();
	int getId();
	string getName();
	string getPwd();
	int getRole();
private:
	int ID;
	string name;
	string pwd;
	int role;
};
 
#endif
 

3.CStaff.cpp

#include"CStaff.h"
#include<iostream>
using namespace std;
 
Staff::Staff()
{
}
 
Staff::Staff(int id,string name,string pwd,int prole)
{
	this->ID = id;
	this->name = name;
	this->pwd = pwd;
	this->role = prole;
}
 
int Staff::getId()
{
	return this->ID;
}
 
string Staff::getName()
{
	return this->name;
}
 
string Staff::getPwd()
{
	return this->pwd;
}
 
int Staff::getRole()
{
	return this->role;
}
 
Staff::~Staff()
{
}
 
 

4.CData.h

#ifndef CDATA_H
#define CDATA_H
#include<list>
#include"CStaff.h"
 
//专门用来做数据准备  文件存储在磁盘中 程序运行在内存中
//缓存区 链表 向量    适合什么样的容器
class CData
{
public:
	//静态:不通过对象 属于类 类名::静态成员/静态函数
	static list<Staff> staffList;
	static void userInit();      //用户数据初始化
};
 
#endif

5.CData.cpp

#include"CData.h"
#include<fstream>
#include<iostream>
using namespace std;
 
list<Staff> CData::staffList; //静态成员的初始化
 
//实现类的静态函数
void CData::userInit()
{
	/*
	1.从文件中读取数据 存入list
	2.如果没有数据 先预定义一些数据写入文件 存储list3个
	3.如果有数据 读取出来存入list
	*/
	fstream fs;//文件流对象  in从文件中读出 out写入文件 app追加
	fs.open("user.txt",fstream::in | fstream::out |fstream::app);
	//目标读文件 文件指示器需要定在开头
	//如果没有数据 定位到文件尾部 获取文件大小
	fs.seekg(0, ios::end);
	//计算文件中的字节数
	int count = fs.tellg();
	//创建一个迭代器
	list<Staff>::iterator it;
	if(count<=0)
	{
		cout<<"没有数据,准备数据,写入文件"<<endl;
		CData::staffList.push_back(Staff(1001,"admin","123",ADMIN));
		CData::staffList.push_back(Staff(1002,"lily","123",MANAGER));
		for(it = CData::staffList.begin();it!=CData::staffList.end();it++)
		{
			//fs写入 每个元素是对象.运算符获取
			//每个数据一行 用空格隔开
			fs<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;
		}
	}
}

结果:

二:读文件操作

CData.cpp

#include"CData.h"
#include<fstream>
#include<iostream>
using namespace std;
 
list<Staff> CData::staffList; //静态成员的初始化
 
//实现类的静态函数
void CData::userInit()
{
	/*
	1.从文件中读取数据 存入list
	2.如果没有数据 先预定义一些数据写入文件 存储list3个
	3.如果有数据 读取出来存入list
	*/
	fstream fs;//文件流对象  in从文件中读出 out写入文件 app追加
	fs.open("user.txt",fstream::in | fstream::out |fstream::app);
	//目标读文件 文件指示器需要定在开头
	//如果没有数据 定位到文件尾部  获取文件大小
	fs.seekg(0, ios::end);
	//计算文件中的字节数
	int count = fs.tellg();
	//创建一个迭代器
	list<Staff>::iterator it;
	if(count<=0)
	{
		cout<<"没有数据,准备数据,写入文件"<<endl;
		CData::staffList.push_back(Staff(1001,"admin","123",ADMIN));
		CData::staffList.push_back(Staff(1002,"lily","123",MANAGER));
		for(it = CData::staffList.begin();it!=CData::staffList.end();it++)
		{
			fs<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;		
		}
	}
	else
	{
		//目标读文件 文件指示器定位到开头
		fs.seekg(0,ios::beg);
		char buf[256] = {0};
		int id = 0,role = 0;
		char pwd[10]={0};
		char name[10]={0};
		while(fs.peek()!=EOF)//EOF是读到末尾
		{
			//没有读到最后 每一行都读取
			fs.getline(buf,256);
			//sscanf读到数据 使用空格进行拆分
			sscanf(buf,"%d %s %s %d",&id,name,pwd,&role);
			//拆分出来的数据 放入链表中
			CData::staffList.push_back(Staff(id,name,pwd,role));		
		}
		for(it = CData::staffList.begin();it!=CData::staffList.end();it++)//验证是否读对
		{
			cout<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;		
		}	
	}	
}

结果:读到的是文件中的正确信息

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们