C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實現(xiàn)
更新時間:2022年06月16日 10:06:17 作者:想吃讀研的苦
本文主要介紹了C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
一:沒有數(shù)據(jù),準備數(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ù)準備 文件存儲在磁盤中 程序運行在內(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)成員的初始化 //實現(xiàn)類的靜態(tài)函數(shù) void CData::userInit() { /* 1.從文件中讀取數(shù)據(jù) 存入list 2.如果沒有數(shù)據(jù) 先預定義一些數(shù)據(jù)寫入文件 存儲list3個 3.如果有數(shù)據(jù) 讀取出來存入list */ fstream fs;//文件流對象 in從文件中讀出 out寫入文件 app追加 fs.open("user.txt",fstream::in | fstream::out |fstream::app); //目標讀文件 文件指示器需要定在開頭 //如果沒有數(shù)據(jù) 定位到文件尾部 獲取文件大小 fs.seekg(0, ios::end); //計算文件中的字節(jié)數(shù) int count = fs.tellg(); //創(chuàng)建一個迭代器 list<Staff>::iterator it; if(count<=0) { cout<<"沒有數(shù)據(jù),準備數(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寫入 每個元素是對象.運算符獲取 //每個數(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)成員的初始化 //實現(xiàn)類的靜態(tài)函數(shù) void CData::userInit() { /* 1.從文件中讀取數(shù)據(jù) 存入list 2.如果沒有數(shù)據(jù) 先預定義一些數(shù)據(jù)寫入文件 存儲list3個 3.如果有數(shù)據(jù) 讀取出來存入list */ fstream fs;//文件流對象 in從文件中讀出 out寫入文件 app追加 fs.open("user.txt",fstream::in | fstream::out |fstream::app); //目標讀文件 文件指示器需要定在開頭 //如果沒有數(shù)據(jù) 定位到文件尾部 獲取文件大小 fs.seekg(0, ios::end); //計算文件中的字節(jié)數(shù) int count = fs.tellg(); //創(chuàng)建一個迭代器 list<Staff>::iterator it; if(count<=0) { cout<<"沒有數(shù)據(jù),準備數(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 { //目標讀文件 文件指示器定位到開頭 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ù) 使用空格進行拆分 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++)//驗證是否讀對 { cout<<(*it).getId()<<" "<<(*it).getName()<<" "<<(*it).getPwd()<<" "<<(*it).getRole()<<endl; } } }
結(jié)果:讀到的是文件中的正確信息
到此這篇關(guān)于C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)C++文件數(shù)據(jù)寫入和讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Objective-C限制函數(shù)調(diào)用的頻率詳解
這篇文章主要給大家介紹了關(guān)于Objective-C限制函數(shù)調(diào)用的頻率的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12