網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實(shí)現(xiàn)_C 語(yǔ)言
作者:想吃讀研的苦 ? 更新時(shí)間: 2022-08-07 編程語(yǔ)言一:沒有數(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"
//專門用來(lái)做數(shù)據(jù)準(zhǔn)備 文件存儲(chǔ)在磁盤中 程序運(yùn)行在內(nèi)存中
//緩存區(qū) 鏈表 向量 適合什么樣的容器
class CData
{
public:
//靜態(tài):不通過(guò)對(duì)象 屬于類 類名::靜態(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ù)寫入文件 存儲(chǔ)list3個(gè)
3.如果有數(shù)據(jù) 讀取出來(lái)存入list
*/
fstream fs;//文件流對(duì)象 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)建一個(gè)迭代器
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寫入 每個(gè)元素是對(duì)象.運(yùn)算符獲取
//每個(gè)數(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ù)寫入文件 存儲(chǔ)list3個(gè)
3.如果有數(shù)據(jù) 讀取出來(lái)存入list
*/
fstream fs;//文件流對(duì)象 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)建一個(gè)迭代器
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);
//拆分出來(lái)的數(shù)據(jù) 放入鏈表中
CData::staffList.push_back(Staff(id,name,pwd,role));
}
for(it = CData::staffList.begin();it!=CData::staffList.end();it++)//驗(yàn)證是否讀對(duì)
{
cout<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl;
}
}
}
結(jié)果:讀到的是文件中的正確信息
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/124395709
相關(guān)推薦
- 2022-06-02 C++超詳細(xì)講解單鏈表的實(shí)現(xiàn)_C 語(yǔ)言
- 2022-12-04 Dart?異步編程生成器及自定義類型用法詳解_Dart
- 2022-11-23 Python多線程使用方法詳細(xì)講解_python
- 2022-05-21 Python中三種花式打印的示例詳解_python
- 2024-03-10 @Controller、@Service和@Repository注解詳解
- 2022-06-26 Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解_Android
- 2022-10-06 Android?Fragment源碼分析Add方法_Android
- 2022-06-25 教你在VS2022?MFC程序中調(diào)用CUDA代碼的方法_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支