日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實(shí)現(xiàn)_C 語言

作者:想吃讀研的苦 ? 更新時間: 2022-08-07 編程語言

一:沒有數(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

欄目分類
最近更新