程序運行時產(chǎn)生的數(shù)據(jù)都屬于零食數(shù)據(jù),程序一旦運行結(jié)束,就會被釋放
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務豐縣,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
通過文件可以將數(shù)據(jù)持久化
C++中對文件的操作包含頭文件
文件類型分為兩種
操作文件的三大類
包含頭文件
#include<fstream>
創(chuàng)建流對象
ofstream ofs;
打開文件
ofs.open("文件路徑",打開方式);
寫數(shù)據(jù)
ofs << "寫入的數(shù)據(jù)";
關(guān)閉文件
ofs.cloSe()
#include<iostream>
#include<fstream> //添加頭文件
using namespace std;
void test01(){
ofstream ofs; //創(chuàng)建流對象
ofs.open("test.txt",ios::out); //打開的地址和方式
ofs << "你好" << endl; //文件的內(nèi)容
ofs.close(); //關(guān)閉文件
}
int main(int argc, char** argv) {
test01();
return 0;
}
步驟相對于寫文件多一點
包含頭文件
#include<fstream>
創(chuàng)建流對象
ifstream ifs;
打開文件并判斷釋放打開成功
ifs.open("文件路徑",打開方式);
if(!ifs.is_open()){
cout << "文件打開失敗" << endl;
return;
}else{...}
讀數(shù)據(jù)
一共四種...
關(guān)閉文件
ifs.cloSe()
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
void test01(){
ifstream ifs;
ifs.open("test.txt",ios::in);
if(!ifs.is_open()){ //判斷是否打開成功
cout << "文件打開失敗" << endl;
return;
}else{
//第一種
// char buf[1024] = {0};
// while(ifs >> buf){ //將ifs的數(shù)據(jù)放入buf中,當數(shù)據(jù)讀完時,返回false,結(jié)束循環(huán)(不讀取數(shù)據(jù))
// cout << buf <<endl;
// }
//第二種
// char buf[1024] = {0};
// while (ifs.getline(buf,sizeof(buf))){ //getline:一行一行的讀取
// cout << buf << endl;
// }
// }
//第三種
// string buf;
// while(getline(ifs,buf)){ //ifs一行一行讀到buf中
// cout << buf << endl;
// }
//第四種(不建議用)
char c;
while((c = ifs.get()) != EOF){ //EOF :文件的尾部標志
cout << c; //不要加endl;
}
ifs.close(); //關(guān)閉文件
}
}
int main(int argc, char** argv) {
test01();
return 0;
}
在上面代碼的基礎(chǔ)上將已經(jīng)創(chuàng)建好的test.txt文件復制到test1.txt文件下
#include<fstream> //添加頭文件
#include<iostream>
#include<cstring>
using namespace std;
void test01(){
ofstream ofs; //創(chuàng)建對象
ifstream ifs;
ifs.open("test.txt",ios::in); //路徑和方式
ofs.open("test1.txt",ios::out);
if(!ifs.is_open()){ //復制過程
cout << "文件打開失敗" << endl; //判斷是否打開成功
return;
}else{
string buf;
while(getline(ifs,buf)){ //讀文件
ofs << buf << "(test.txt復制成功)" << endl; //寫(復制)文件
// 添加標識,判斷復制是否成功
}
}
ofs.close(); //關(guān)閉文件
ifs.close();
}
int main(int argc, char** argv) {
test01();
return 0;
}
在以二進制讀、寫文件的時候要加多加一個‘ios::binary’
二進制方式讀文件主要利用流對象調(diào)用函數(shù)write(讀出的部分數(shù)據(jù)看不懂沒有關(guān)系,只要還能讀回來就可以啦)
函數(shù)類型:ofs.write((const char *) &buffer,int len);
參數(shù)解釋:buffer指向內(nèi)存中的一段數(shù)據(jù);len是要讀取的字節(jié)數(shù)
#include<fstream>
#include<iostream>
using namespace std;
class Person{ //寫個類讀讀
public:
char m_Name[64]; //最好用char
int m_Age;
};
void test01(){
ofstream ofs("Person.txt",ios::binary | ios::out); //可以一步寫哦
//ofs.open("Person.txt",ios::binary | ios::out);
Person p = {"Tom" ,18}; //造個對象
ofs.write((const char*)&p,sizeof(p)); //用write函數(shù)讀出來
ofs.close();
}
int main(int argc, char** argv) {
test01();
return 0;
}
二進制方式讀文件主要利用流對象調(diào)用函數(shù)read
函數(shù)類型:ofs.read((const char *) &buffer,int len);
參數(shù)解釋:buffer指向內(nèi)存中的一段數(shù)據(jù);len是要讀取的字節(jié)數(shù)
#include<fstream> //是同一個Person,年齡是20咯(類哦)
#include<iostream> //我把讀的也寫進來了,這樣更靈活一點
using namespace std;
class Person{
public:
char m_Name[64];
int m_Age;
};
void test01(){
//寫文件
ofstream ofs("Person.txt",ios::binary | ios::out); //可以一步寫哦
//ofs.open("Person.txt",ios::binary | ios::out);
Person p = {"Tom" 20}; //改個對象的參數(shù)()
ofs.write((const char*)&p,sizeof(p));
ofs.close();
//讀文件
ifstream ifs("Person.txt",ios::binary | ios::in);
if(!(ifs.is_open())){
cout << "文件打開失敗" << endl;
return;
}else{
Person p;
ifs.read((char*)&p,sizeof(p));
cout << p.m_Name << p.m_Age <<endl;
}
ifs.close();
}
int main(int argc, char** argv) {
test01();
return 0;
}
為進一步驗證是對同一個文件操作,可以做如下檢驗
else{
Person p = {"Petter",250};//再次修改
ifs.read((char*) &p,sizeof(p));
cout << p.m_Name << p.m_Age <<endl;
}
分享文章:C++基礎(chǔ)-文件操作
轉(zhuǎn)載來源:http://chinadenli.net/article24/dsoipje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司、響應式網(wǎng)站、靜態(tài)網(wǎng)站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)