c++文件操作

c++文件操作:

文件操作三大步(邏輯如下):

1、打開(kāi)文件

2、讀寫文件

3、關(guān)閉文件

介紹下頭文件:

#include<fstream> //文件讀寫頭文件

記憶如下:

? ? ?a、文件:file

? ? ?b、流:stream

合并就是:fstream

小貼士:

輸出:程序向文件或者顯示器或其他設(shè)備輸出內(nèi)容

輸入:程序從文件或者設(shè)備中讀取內(nèi)容稱之為輸入

(cin:輸入,cout:輸出;讀取文件:輸入,寫入文件:輸出(注意下概念))

想看結(jié)果的直接跳轉(zhuǎn)完整代碼:

讀取文件過(guò)程如下——:

1、建立對(duì)象(注意理解哦):

ifstream in;? ? ?//輸入對(duì)象——文件讀

ofstream out; //輸出對(duì)象——文件寫

fstream? fs;? ?//輸入輸出對(duì)象——可讀可寫

2、打開(kāi)文件

in.open(這里填寫文件路徑,打開(kāi)模式);

out.open(這里填寫文件路徑,打開(kāi)模式);

out.open(這里填寫文件路徑,打開(kāi)模式);??

第一個(gè)參數(shù):文件路徑

第二個(gè)參數(shù):打開(kāi)模式

ios::app?Opens?an?output?file?for?appending.?ios::ate?Opens?an?existing?file?(either?input?or?output)?and?seeks?the?end.??ios::in?Opens?an?input?file.?Use?ios::in?as?an?open_mode?for?an?ofstream?file?to?prevent?truncating?an?existing?file.??ios::out?Opens?an?output?file.?When?you?use?ios::out?for?an?ofstream?object?without?ios::app,?ios::ate,?or?ios::in,?ios::trunc?is?implied.?ios::nocreate?Opens?a?file?only?if?it?already?exists;?otherwise?the?operation?fails.?ios::noreplace?Opens?a?file?only?if?it?does?not?exist;?otherwise?the?operation?fails.?ios::trunc?Opens?a?file?and?deletes?the?old?file?(if?it?already?exists).?ios::binary?Opens?a?file?in?binary?mode?(default?is?text?mode).??

翻譯(還是看英文舒服):

ios::app? ? 打開(kāi)輸出(寫入)文件供追加。

ios::ate? ? ?打開(kāi)一個(gè)已存在文件(輸入(讀)或輸出(寫))并定位到文件尾。

ios::in? ? ? ?打開(kāi)一個(gè)輸入(讀)文件,使用ios::in作為輸出流的打開(kāi)模式來(lái)防止截?cái)? ? ? ? ? ? ? ? ? ?現(xiàn)有文件

ios::out? ? 打開(kāi)一個(gè)輸出文件(寫),當(dāng)使用ios::out沒(méi)有ios::app、ios::ate或ios::in? ? ? ? ? ? ? ? ? ? 的ofstream對(duì)象時(shí),ios::trunc是隱含的。

ios::nocreate?僅打開(kāi)一個(gè)已存在的文件;否則操作失敗.??

ios::noreplace 僅打開(kāi)一個(gè)不存在的文件,否則操作失敗.?

ios::trunc 打開(kāi)一個(gè)文件,如果已存在,則刪除舊文件.??

ios::binary? 以二進(jìn)制打開(kāi)文件(默認(rèn)為文本模式).???

ifstream 一般組合方式(打開(kāi)模式):

? ? ? 1、ios::in |ios::binary? ? // 二進(jìn)制打開(kāi)(常用用于文件流)

? ? ? 2、ios::in? ? ? ? ? ? ? ? ? ? ? //打開(kāi)文件

? ? ? 3、ios::ate? ? ? ? ? ? ? ? ? //打開(kāi)文件定位到數(shù)據(jù)尾

ofstream 一般組合方式(打開(kāi)模式):

? ? ? 1、ios::app或ios::app|ios::out? ? ? ? //無(wú)文件則創(chuàng)建,有文件則追加

? ? ? 2、ios::ate或ios::ate|ios::out? ? ? ? ? //無(wú)文件則生成,有文件則清空

? ? ? 3、?ios::ate|ios::in? ? ? ? ? ? ? ? ? ? ? ? ? ?//無(wú)文件,則失敗,有文件,則追加

fstream 一般組合方式(打開(kāi)模式):?

? ? ? 1、ios::app|ios::out? ? ? ? ? ? ? ? ? ? //寫,無(wú)文件,則創(chuàng)建。有文件,則追加

? ? ? 2、ios::app|ios::in? ? ? ? ? ? ? ? ? ? ? ?//讀,不存在則打開(kāi)失敗? ?

? ? ? 3、ios::ate|ios::out? ? ? ? ? ? ? ? ? ? ? //無(wú)則創(chuàng)建,有則清空

? ? ? 4、ios::ate|ios::in? ? ? ? ? ? ? ? ? ? ?? //讀,不存在則打開(kāi)失敗

? ? ?5、ios::ate|ios::in|ios::out? ? ? ? ? ? ?//可讀可寫,指針置于文件尾部

小貼士:注意下打開(kāi)模式(讀還是寫,會(huì)有沖突哦~)

介紹:

? ? ? ?文件路徑都知道,什么可說(shuō)的,打開(kāi)模式,自行理解

//通過(guò)移動(dòng)文件讀寫指針,可在文件指定位置進(jìn)行讀寫。 //seekg(絕對(duì)位置);      //絕對(duì)移動(dòng),    //輸入流操作 //seekg(相對(duì)位置, 參照位置);  //相對(duì)操作 //tellg();          //返回當(dāng)前指針位置 //seekp(絕對(duì)位置);      //絕對(duì)移動(dòng),    //輸出流操作 //seekp(相對(duì)位置, 參照位置);  //相對(duì)操作    //tellp();          //返回當(dāng)前指針位置 //參照位置: //ios::beg   = 0       //相對(duì)于文件頭 //ios::cur   = 1       //相對(duì)于當(dāng)前位置 //ios::end   = 2       //相對(duì)于文件尾

完整案例(以二進(jìn)制為例):

~~~

#include<iostream>

#include<fstream>

using namespace std;

typedef struct student {

int age = 0;

string name;

}stu;

int main() {

long temp = 0;

stu a;

a.age = 10;

a.name = "大寶";

stu b;

b.age = 88;

b.name = "sod";

//創(chuàng)建寫文件對(duì)象(輸出)

ofstream out;

out.open("E:/c++教學(xué)/c++_four/four.txt", ios::app | ios::out | ios::binary); //追加模式寫

if (out.is_open() == false) {

cout << "打開(kāi)失敗";

return 0;

}

else {

out.write((char*)& a, sizeof(a));//不兼容(強(qiáng)制轉(zhuǎn)換)

out.write((char*)& b, sizeof(b));//不兼容(強(qiáng)制轉(zhuǎn)換)

out.close();

}

//寫結(jié)束

//定義c,用來(lái)接收讀取的數(shù)據(jù)

stu c;

//創(chuàng)建讀文件對(duì)象(輸入)

ifstream in;

in.open("e:/c++教學(xué)/c++_four/four.txt", ios::in | ios::binary);//in是讀

if (in.is_open() == false) {

cout << "打開(kāi)失敗";

return 0;

}

else {

in.seekg(0, ios::end);//將指針移到未見(jiàn)末尾

long length = in.tellg();

in.seekg(0, ios::beg);//將指針移到文件頭

long k = length / sizeof(c);

for (int i = 0; i < k; i++) {

? ? temp = sizeof(c) * i;

in.seekg(temp);

in.read((char*)& c, sizeof(c));

cout << c.name;

cout << c.age;

cout << endl;

}

in.close();

}

cout << "操作結(jié)束";

return 0;

}

~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容