溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

流類庫(kù)和文件

發(fā)布時(shí)間:2020-07-24 17:08:58 來(lái)源:網(wǎng)絡(luò) 閱讀:367 作者:匯天下豪杰 欄目:移動(dòng)開(kāi)發(fā)

1、流類庫(kù)

  (1)、C++中沒(méi)有輸入輸出,標(biāo)準(zhǔn)庫(kù)中包含一個(gè)I/O流類庫(kù)。

  C語(yǔ)言中printf 和scanf 稱為函數(shù); 輸出到屏幕

  C++中cout和cin稱為對(duì)象;  輸入是鍵盤

  其中iostream是虛繼承;

  cout<<int/char/double----->是因?yàn)橹剌d了<<。

流類庫(kù)和文件

  (2)、C++流類庫(kù)中定義4個(gè)全局流對(duì)象,cin、cout、cerr、clog

  cin 標(biāo)準(zhǔn)輸入流對(duì)象,鍵盤

  cout標(biāo)準(zhǔn)輸出流,屏幕

  cerr和clog標(biāo)準(zhǔn)錯(cuò)誤輸出流, 屏幕

  其中cin、cout、clog是帶緩沖區(qū)的,緩沖區(qū)由streambuf類對(duì)象來(lái)管理,cerr非緩沖區(qū),一旦錯(cuò)誤發(fā)生立即顯示。

#include<iostream>
using namespace std;

int main(void){
    cerr<<"Error"<<endl; //錯(cuò)誤直接輸出
    cout<<"Hello"<<endl; //先放到緩沖區(qū)中
}

2、格式控制

  會(huì)用,知道怎么查就行了,沒(méi)必要記這些;

  cout.flags(ios::hex); //hex這些在ios類中,以16進(jìn)制輸出;

流類庫(kù)和文件

0000 0000 0000 0000  有多少個(gè)1,就有什么功能;

ios::hex | ios::showbase        hex和showbase都是在ios類中定義的枚舉,1-16各占一位;

3、文件

  (1)、先定義一個(gè)文件對(duì)象流

  (2)、打開(kāi)某個(gè)文件

  (3)、進(jìn)行文件的讀寫(xiě)

  (4)、關(guān)閉文件

ASCII字符節(jié)文件:

  fprintf(fd, "", );    :寫(xiě)入文件

  fscanf(fd, "", );    :從文件讀出

文本文件的操作,寫(xiě)入文件:

#include<iostream>
#include<fstream>//文件輸出流頭文件
#include<stdlib.h>
using namespace std;

int main(void){
    int ar[] = {1, 3, 5, 6, 7, 9,};
    //1、
    //ofstream ofile("Test1.txt", ios::out);與下2步等價(jià)
    ofstream ofile;
    //2、
    ofile.open("Test1.txt", ios::out);
    if(!ofile){
        cerr<<"Open File Fail!"<<endl;
        exit(1);
    }
    for(int i = 0; i < sizeof(ar)/sizeof(int); i++){
        ofile<<ar[i]<<" ";
    }

    ofile.close();
}

  在文件中讀出:

#include<iostream>
#include<fstream>//文件輸出流頭文件
#include<stdlib.h>
using namespace std;

int main(void){
    int ar[10];
    ifstream ifile;
    ifile.open("Test1.txt", ios::in);
    if(!ifile){
        cerr<<"Open File Fail"<<endl;
        exit(1);
    }
    for(int i = 0; i < 4; i++){
        ifile>>ar[i];
    }

    ifile.close();
}

4、二進(jìn)制讀寫(xiě)

  寫(xiě)入文件:

#include<iostream>
#include<fstream>//文件輸出流頭文件
#include<stdlib.h>
using namespace std;

int main(void){
    int ar[] = {1, 3, 5, 6, 7, 9,};
    //1、
    //ofstream ofile("Test1.txt", ios::out);與下2步等價(jià)
    ofstream ofile;
    //2、
    ofile.open("Test1.txt", ios::out | ios::binary);
    if(!ofile){
        cerr<<"Open File Fail!"<<endl;
        exit(1);
    }
    ofile.write((char *)ar, sizeof(ar));//這樣就不用循環(huán)了,一次性解決

    ofile.close();
}

  從文件讀出:

#include<iostream>
#include<fstream>//文件輸出流頭文件
#include<stdlib.h>
using namespace std;

int main(void){
    int ar[] = {1, 3, 5, 6, 7, 9,};
    //1、
    //ifstream ofile("Test1.txt", ios::out);
    ifstream ifile;
    //2、
    ifile.open("Test1.txt", ios::out | ios::binary);
    if(!ifile){
        cerr<<"Open File Fail!"<<endl;
        exit(1);
    }
    ifile.read((char *)ar, sizeof(ar));//這樣就不用循環(huán)了,一次性全部讀出

    ifile.close();
}

5、文件隨機(jī)訪問(wèn)

  隨機(jī)讀寫(xiě)關(guān)鍵靠文件指針;

  文件指針,開(kāi)始指向第一個(gè),讀寫(xiě)就靠這個(gè)文件讀寫(xiě)指針,會(huì)自動(dòng)指向下一個(gè);

#include<iostream>
#include<fstream>//文件輸出流頭文件
#include<stdlib.h>
using namespace std;

int main(void){
    ifstream ifile;

    ifile.open("Test1.txt", ios::in);
    if(!ifile){
        cerr<<"Open File Fail!"<<endl;
        exit(1);
    }
    int pos;
    int value;
    while(1){
        cout<<"請(qǐng)輸入位置: ";
        cin>>pos;
        //beg  cur  end
        ifile.seekg(pos, ios::beg);//定位函數(shù),pos,相對(duì)于什么地方開(kāi)始
        ifile>>value; //將定位處的值放入value;
        cout<<"value = "<<value<<endl;
    }
    ifile.close();
}

文件可以定位讀出,最好用二進(jìn)制解決,每個(gè)數(shù)字都是4字節(jié);就不用考慮每個(gè)數(shù)字跨幾個(gè)字節(jié),所以為pos*4;

文本文件在其中每個(gè)數(shù)字(0-9)占用1個(gè)字節(jié),不好定位一個(gè)完整數(shù)字占用幾個(gè)字節(jié);

 5、文件與對(duì)象

  在C++程序設(shè)計(jì)中,文件應(yīng)該在構(gòu)造函數(shù)中打開(kāi),并創(chuàng)建對(duì)象,而在析構(gòu)函數(shù)中保存和關(guān)閉文件,并撤銷對(duì)象;

  對(duì)文件而言,釋放資源的同時(shí)包括將對(duì)象中的信息再次存入磁盤文件,在程序運(yùn)行過(guò)程中,應(yīng)將信息適時(shí)保存到相應(yīng)

的磁盤文件,以免數(shù)據(jù)意外丟失。

  文件與對(duì)象的有機(jī)結(jié)合(關(guān)鍵在構(gòu)造和析構(gòu)函數(shù)),以下就是一個(gè)相應(yīng)的例子:

#include<iostream>
#include<fstream>
using namespace std;

class Complex;
ostream& operator<<(ostream &out, const Complex &t);

class Complex{
    friend ostream& operator<<(ostream &out, const Complex &t);
public:
    Complex() : real(0), p_w_picpath(0){
        ifstream ifile;
        ifile.open("Test.txt", ios::in);
        if(!ifile){
            cerr<<"Open File Fail"<<endl;
            exit(1);
        }
        ifile>>real>>p_w_picpath;
        ifile.close();
    }
    Complex(int rea, int imag) : real(rea), p_w_picpath(imag){}
    ~Complex(){
        ofstream ofile;
        ofile.open("Test.txt", ios::out);
        if(!ofile){
            cerr<<"Open File Fail"<<endl;
            exit(1);
        }
        ofile<<real<<" "<<p_w_picpath;
        ofile.close();
    }
public:
    void setComplex(int real, int p_w_picpath){
        this->real = real;
        this->p_w_picpath = p_w_picpath;
    }
private:
    int real;
    int p_w_picpath;
};

ostream& operator<<(ostream &out, const Complex &t){
    out<<"("<<t.real<<","<<t.p_w_picpath<<")";
    return out;
}
int main(void){
    Complex c;
    cout<<c<<endl;
    c.setComplex(100, 200);//模擬了在下一次實(shí)例化時(shí)把上一次讀取出來(lái)。
//    cout<<c<<endl;//寫(xiě)進(jìn)文件,保存設(shè)置。
}



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI