欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:c++ 16-IO

柚子快報激活碼778899分享:c++ 16-IO

http://yzkb.51969.com/

文件IO

C++,對C語言的文件IO流操作進(jìn)行了封裝,包含頭文件:#include

文件輸出流

在C++中,寫入文本文件主要通過類:ofstream(output file stream)來完成。而寫入文件文件之前,首先要打開一個文件。C++把打開文件的過程封裝在了構(gòu)造函數(shù)中,而對打開文件的模式進(jìn)行了約束:

ios::out 缺省值:會截斷文件內(nèi)容。 等價于 truncios::trunc 截斷文件內(nèi)容。(truncate)ios::app 不截斷文件內(nèi)容,只在文件未尾追加文件。(append)對于 ofstream,不管用哪種模式打開文件,如果文件不存在,都會創(chuàng)建出文件。

class fstream{

public:

//文件名可以使用C風(fēng)格字符串

fstream( const char *filename, openmode mode );

//也可使用C++的string類

fstream( const string &filename, openmode mode );

}

針對fileName ,C++做到了最大程度的適配工作。

string fileName = "D://test.txt"; //轉(zhuǎn)義字符

string fileName = R"(D:\test.txt)"; //C++標(biāo)準(zhǔn)

string fileName = "D:/test.txt"; //斜線反著寫

string fileName = "/data/test.txt"; //Linux 風(fēng)格

char fileName[] = "D://test.txt"; //C風(fēng)格字符串

下面我們來討論下不同打開模式的區(qū)別。

默認(rèn)情況下,構(gòu)造方法不寫第二個參數(shù),等價于openmode=ios::out向文件中寫入文件是的openmode 是會截斷文件的內(nèi)容的,意思就是你里面不管有多少內(nèi)容都會被覆蓋掉。

string fileName = "D://test.txt";

// ofstream fout(fileName, ios::out); 等價寫法

ofstream fout(fileName);

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

cout << "打開文件" << fileName << "失敗了" << endl;

return 0;

}

fout << "wangnaixing" << endl;

fout << "huangjieying" << endl;

fout << "zhangshan" << endl;

fout.close();

如果此時我們希望,不要截斷文件內(nèi)容,我們可以更改openmode=app,這樣每一個寫入的內(nèi)容都會直接在文鍵原內(nèi)容末尾追加了。

...

ofstream fout(fileName, ios::app);

...

文件輸入流

在C++中,讀取文本文件使用ifstream 對象。同樣的ofstream類讀取文件文件一樣,ifstream需要一個打開模式。

class ifstream{

public:

//和ofstream 一樣

ifstream( const char *filename, openmode mode );

ifstream(const string &filename, openmode mode );

}

再結(jié)合getline() 方法和WHILE 循環(huán) 就可以將數(shù)據(jù)都寫到 string 中。

#include

#include

#include

using namespace std;

int main()

{

ifstream fin;

string fileName = R"(D:\test.txt)";

fin.open(fileName, ios::in);

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

cout << "打開文件" << fileName << "失敗" << endl;

return 0;

}

string buffer;

while (getline(fin, buffer))

{

cout << buffer << endl;

}

fin.close();

}

和寫入文件不同的是,假如文件不存在。則使用ifstream 打開它,會失敗。

ifstream fin;

string fileName = R"(D:\test2.txt)"; //test2在磁盤中

fin.open(fileName,ios::in);

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

cout << "打開文件" << fileName << "失敗" << endl;

return 0;

}

fin.close();

在讀取文件內(nèi)容還可以用一個C風(fēng)格字符串作為接受。但是需要考慮一點的是,buffer[] 的空間一定要足夠大。

....

char buffer[16];

while (fin.getline(buffer,15))

{

cout << buffer <

}

fin.close();

還用一種是直接用 >> 右移運算符的。這樣看著更為簡潔。在實際開發(fā)中我們更常用這種方式。

....

string buffer;

while (fin >> buffer)

{

cout << buffer << endl;

}

fin.close();

二進(jìn)制文件讀取

二進(jìn)制文件以數(shù)據(jù)塊的形式組織數(shù)據(jù),把內(nèi)存中的數(shù)據(jù)直接寫入文件。

ofstream fout;

char fileName[] = "D:/text.txt";

fout.open(fileName, ios::app | ios::binary);

if (fout.is_open() == false)

{

cout << "打開文件" << fileName << "失敗了" << endl;

}

struct st_girl {

char name[31];

int no;

char memo[301];

double weight;

}girl;

girl = { "西施",3,"中國歷史第一美女。" ,45.8 };

fout.write((const char*)&girl, sizeof(st_girl)); // 寫入第一塊數(shù)據(jù)

girl = { "冰冰",8,"也是個大美女哦。",55.2 };

fout.write((const char*)&girl, sizeof(st_girl)); // 取結(jié)構(gòu)體地址

fout.close();

如何讀取二進(jìn)制的文件呢?那么你需要定義和寫入結(jié)構(gòu)體一樣的格式,去讀取。

char fileName[] = "D:/text.txt";

ifstream fin;

fin.open(fileName,ios::in|ios::binary);

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

cout << "打開文件" << fileName << "失敗\n" << endl;

return 0;

}

struct st_girl {

char name[31];

int no;

char memo[301];

double weight;

}girl;

while (fin.read((char*)&girl,sizeof(girl)))

{

cout << "name=" << girl.name << ",no=" << girl.no <<

",memo=" << girl.memo << ",weight=" << girl.weight << endl;

}

fin.close();

文件流

fstream 類既可以讀文本/二進(jìn)制文件,也可以寫文本/二進(jìn)制文件。

fstream 類的缺省模式是 ios::in | ios::out,如果文件不存在,則創(chuàng)建文件;但是,不會清空文件原有的內(nèi)容。

普遍的做法是 如果只想寫入數(shù)據(jù),用 ofstream;如果只想讀取數(shù)據(jù),用 ifstream;如果想寫和讀數(shù)據(jù),用 fstream.

比如我想讀一下文本文件

char fileName[] = "D:/test.txt";

fstream fsm;

fsm.open(fileName,ios::in);

string buffer;

while (fsm >> buffer)

{

cout << buffer << endl;

}

比如我想寫點數(shù)據(jù)到文本文件,

char fileName[] = "D:/test.txt";

fstream fsm;

//fsm.open(fileName,ios::app); 追加模式

fsm.open(fileName,ios::out);

fsm << "HelloWorld";

fsm.close();

文件位置指針

當(dāng)我們對文件進(jìn)行寫操作的時候,C++ 的ofstream 內(nèi)部就會維護一個位置指針,指向當(dāng)前文件的寫位置。

同理當(dāng)我們對文件進(jìn)行讀操作的時候,C++的 ifstream 內(nèi)部也會維護一個位置指針,指向當(dāng)前文件的讀位置。

如果我們想獲取到這個指針,ofstream類則使用實例方法tellp()來獲取到。ifstream類的則使用實例方法ellg()來獲取到。

因為fstream類即支持寫文本操作也支持讀文本操作,所以fstream 內(nèi)部會維護兩個指針,一個寫位置指針,一個讀位置指針。所以想獲取到他們則可以使用實例方法 tellp() 來拿到寫指針,使用實例方法ellg() 來拿到讀指針。

class ofstream { //文件輸出流 能寫

public:

// 獲取寫文件的位置指針

std::streampos tellp();

}

class ifstream { //文件輸入流 能讀

public:

// 獲取讀文件的位置指針

std::streampos ellg();

}

class fstream{ // 文件流 能讀能寫

public:

std::streampos tellp();

std::streampos ellg();

}

以ofstream 寫入文本文件為例子,分別獲取到開始寫入文件時,文件寫指針的位置。執(zhí)行寫出5個字符(40個字節(jié)),再去獲取文件寫指針的的位置,可以看到地址正好時所在內(nèi)存空間剛好和開始相差40個字節(jié)。

int main()

{

char fileName[] = "D:/test.txt";

fstream fsm;

fsm.open(fileName,ios::out);

std::streampos startFilePointerPostion = fsm.tellg();

printf("start memery position: %p\n", startFilePointerPostion);

fsm << "Hello";

std::streampos endFilePointerPostion = fsm.tellg();

printf("start memery position: %p\n", endFilePointerPostion);

fsm.close();

}

如果我們希望要移動文件指針的位置呢?

class ofstream{ //文件輸出流

public:

//移動文件讀指針

std::istream & seekp(std::streampos _Pos);

}

class ifstream{ // 文件輸入流

public:

//移動文件寫讀指針

std::istream & seekg(std::streamoff _Off,std::ios::seekdir _Way)

}

class fstream{ //文件流

public:

//移動文件讀指針

std::istream & seekp(std::streampos _Pos);

//移動文件寫讀指針

td::istream & seekg(std::streamoff _Off,std::ios::seekdir _Way)

}

比如我們把寫指針向后移動第5個字節(jié)。再寫入內(nèi)容。打開寫入的文本文件,會發(fā)現(xiàn)前面空了5個字符,內(nèi)容才會被寫入。

...

fout.seekp(5);

fsm << "Hello World";

...

同理我們讀指針向后移動5個字節(jié)開始,再讀取內(nèi)容,可以看到Hello 這5個字節(jié)不會被讀取到。

int main()

{

ifstream fin("D:/test.txt",ios::in);

string buffer;

fin.seekg(5);

while (fin >> buffer)

{

cout << buffer << endl;

}

fin.close();

}

C++ 還支持對 seekp() seekg() 方法進(jìn)行了重載。支持第二個參數(shù),即從什么地方開始。他是ofstream

ifstream 類內(nèi)定義的枚舉類型

class ofstream{

public:

std::istream & seekp(std::streamoff _Off,std::ios::seekdir _Way);

enum seek_dir {

beg, // beg-文件的起始位置

cur, // cur-文件的當(dāng)前位置

end // end-文件的結(jié)尾位置。

};

}

class ifstream{

public:

std::istream & seekg(std::streamoff _Off,std::ios::seekdir _Way);

}

fin.seekg(30, ios::beg); // 從文件【開始的位置】,把讀指針往后移30字節(jié)。

fin.seekg(-5, ios::cur); // 從【當(dāng)前位置】,把讀指針往前移5字節(jié)。

fin.seekg( 8, ios::cur); // 從【當(dāng)前位置】,把讀指針往后移8字節(jié)。

fin.seekg(-10, ios::end); // 從【文件結(jié)尾的位置】,把讀指針往前移10字節(jié)。

...

fout.seekg(...)

文件緩存區(qū)

文件緩沖區(qū)(緩存)是系統(tǒng)預(yù)留的內(nèi)存空間,用于存放輸入或輸出的數(shù)據(jù)。

根據(jù)輸出和輸入流,分為輸出緩沖區(qū)和輸入緩沖區(qū)。

在C++中,每打開一個文件,系統(tǒng)就會為它分配緩沖區(qū)。不同的流,緩沖區(qū)是獨立的。程序員不用關(guān)心輸入緩沖區(qū),只關(guān)心*輸出緩沖區(qū)就行了。

字符串流

在實際開發(fā)中,我們進(jìn)行要將一個基本數(shù)據(jù)類型、或者C風(fēng)格字符串的變量,轉(zhuǎn)換為 string 類型。我們可以使用 include 下的ostringstream 對象來轉(zhuǎn)換。

#include

#include

#include

#include

using namespace std;

int main()

{

// 將Dobule 轉(zhuǎn)為 string

stringstream stream;

double a = 30.345;

stream << a;

string temp1;

stream >> temp1;

cout << temp1 << endl;

// 將 int 轉(zhuǎn)為 string

stream.clear();

int b = 20;

string temp2;

stream << b;

stream >> temp2;

cout << temp2 << endl;

// 將 char* 轉(zhuǎn)為 string

stream.clear();

char cStr[] = "HelloWorld";

string temp3;

stream << cStr;

stream >> temp3;

cout << temp3 << endl;

}

進(jìn)制轉(zhuǎn)換

如果需要對一個整形數(shù)據(jù)(默認(rèn)是十進(jìn)制)進(jìn)行數(shù)據(jù)轉(zhuǎn)換的話,可以使用 include 搭配上 cout 進(jìn)行轉(zhuǎn)換。比如將十進(jìn)制數(shù)據(jù)轉(zhuǎn)為八進(jìn)制、十六進(jìn)制。

#include

#include

#include

using namespace std;

int main()

{

int x = 25, y = 120;

cout << "dec:" << dec << setw(10) << x << setw(10) << y << endl; //dec為十進(jìn)制

cout << "oct:" << oct << setw(10) << x << setw(10) << y << endl; //oct為八進(jìn)制

cout << "hex:" << hex << setw(10) << x << setw(10) << y << dec << endl;//hex為十六進(jìn)制

}

如果要把一個將10進(jìn)制數(shù)據(jù)轉(zhuǎn)為2進(jìn)制數(shù)據(jù),可以使用 include 的bitset() 方法。

#include

#include

#include

#include

using namespace std;

int main()

{

cout << bitset<8>(10) << endl;

}

如果我們想控制,輸出數(shù)據(jù)的精度,使用setprecision(int n)可控制輸出流顯示浮點數(shù)的數(shù)字個數(shù)(包括整數(shù)部分)。C++默認(rèn)的流輸出數(shù)值有效位是6。使用如下:

cout << setprecision(3) << 22.123 << endl; //結(jié)果為:22.1

cout << setprecision(8) << 22.123 << endl; //結(jié)果為:22.123,比原字符長不會補零!

控制臺輸入輸出

通過setf() 指定數(shù)據(jù)的格式化方法為ios::showpos 可以輸出數(shù)據(jù)的符號位。

int main()

{

cout.setf(ios::showpos);

cout << -509.3 << endl;

}

柚子快報激活碼778899分享:c++ 16-IO

http://yzkb.51969.com/

推薦閱讀

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19012386.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機訪問

文章目錄