網(wǎng)站首頁 編程語言 正文
一:沒有數(shù)據(jù),準(zhǔn)備數(shù)據(jù),寫入文件
1.main.cpp
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
#include"CData.h"
#include"CStaff.h"
int main()
{
CData::userInit();//數(shù)據(jù)初始化
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"
//專門用來做數(shù)據(jù)準(zhǔn)備 文件存儲在磁盤中 程序運(yùn)行在內(nèi)存中
//緩存區(qū) 鏈表 向量 適合什么樣的容器
class CData
{
public:
//靜態(tài):不通過對象 屬于類 類名::靜態(tài)成員/靜態(tài)函數(shù)
static list<Staff> staffList;
static void userInit(); //用戶數(shù)據(jù)初始化
};
#endif
5.CData.cpp
#include"CData.h"
#include<fstream>
#include<iostream>
using namespace std;
list<Staff> CData::staffList; //靜態(tài)成員的初始化
//實(shí)現(xiàn)類的靜態(tài)函數(shù)
void CData::userInit()
{
/*
1.從文件中讀取數(shù)據(jù) 存入list
2.如果沒有數(shù)據(jù) 先預(yù)定義一些數(shù)據(jù)寫入文件 存儲list3個
3.如果有數(shù)據(jù) 讀取出來存入list
*/
fstream fs;//文件流對象 in從文件中讀出 out寫入文件 app追加
fs.open("user.txt",fstream::in | fstream::out |fstream::app);
//目標(biāo)讀文件 文件指示器需要定在開頭
//如果沒有數(shù)據(jù) 定位到文件尾部 獲取文件大小
fs.seekg(0, ios::end);
//計(jì)算文件中的字節(jié)數(shù)
int count = fs.tellg();
//創(chuàng)建一個迭代器
list<Staff>::iterator it;
if(count<=0)
{
cout<<"沒有數(shù)據(jù),準(zhǔn)備數(shù)據(jù),寫入文件"<<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寫入 每個元素是對象.運(yùn)算符獲取
//每個數(shù)據(jù)一行 用空格隔開
fs<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;
}
}
}
結(jié)果:
二:讀文件操作
CData.cpp
#include"CData.h"
#include<fstream>
#include<iostream>
using namespace std;
list<Staff> CData::staffList; //靜態(tài)成員的初始化
//實(shí)現(xiàn)類的靜態(tài)函數(shù)
void CData::userInit()
{
/*
1.從文件中讀取數(shù)據(jù) 存入list
2.如果沒有數(shù)據(jù) 先預(yù)定義一些數(shù)據(jù)寫入文件 存儲list3個
3.如果有數(shù)據(jù) 讀取出來存入list
*/
fstream fs;//文件流對象 in從文件中讀出 out寫入文件 app追加
fs.open("user.txt",fstream::in | fstream::out |fstream::app);
//目標(biāo)讀文件 文件指示器需要定在開頭
//如果沒有數(shù)據(jù) 定位到文件尾部 獲取文件大小
fs.seekg(0, ios::end);
//計(jì)算文件中的字節(jié)數(shù)
int count = fs.tellg();
//創(chuàng)建一個迭代器
list<Staff>::iterator it;
if(count<=0)
{
cout<<"沒有數(shù)據(jù),準(zhǔn)備數(shù)據(jù),寫入文件"<<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
{
//目標(biāo)讀文件 文件指示器定位到開頭
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讀到數(shù)據(jù) 使用空格進(jìn)行拆分
sscanf(buf,"%d %s %s %d",&id,name,pwd,&role);
//拆分出來的數(shù)據(jù) 放入鏈表中
CData::staffList.push_back(Staff(id,name,pwd,role));
}
for(it = CData::staffList.begin();it!=CData::staffList.end();it++)//驗(yàn)證是否讀對
{
cout<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;
}
}
}
結(jié)果:讀到的是文件中的正確信息
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/124395709
相關(guān)推薦
- 2022-03-19 MongoDB數(shù)據(jù)庫授權(quán)認(rèn)證的實(shí)現(xiàn)_MongoDB
- 2022-06-11 kubernetes實(shí)現(xiàn)分布式限流_云和虛擬化
- 2022-08-01 MongoDB基礎(chǔ)之集合操作_MongoDB
- 2022-06-30 MongoDB排序時內(nèi)存大小限制與創(chuàng)建索引的注意事項(xiàng)詳解_MongoDB
- 2022-06-19 dockerfile指令構(gòu)建docker鏡像的示例代碼_docker
- 2022-03-30 .NET?Core使用EF生成數(shù)據(jù)庫出錯的解決方法_實(shí)用技巧
- 2022-03-27 Android使用kotlin實(shí)現(xiàn)多行文本上下滾動播放_Android
- 2023-07-27 element plus tabel 全選 保持?jǐn)?shù)據(jù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支