ignore,putback,peek文件

※標(biāo)準(zhǔn)流的設(shè)備名

標(biāo)準(zhǔn)流的設(shè)備名.png

※成員函數(shù)ignore

#include<iostream>
using namespace std;
int main()
{
    string s;
    cout<<"請(qǐng)輸入一串字符串";
    //1.忽略輸入緩存區(qū)中的前8個(gè)字符 
    //2.在前8個(gè)字符中存在結(jié)束字符,那么就忽略 
    //輸入緩沖區(qū) 結(jié)束字符 之前的字符 
    cin.ignore(8,' ');//設(shè)置' '為結(jié)束字符 
    cin>>s;
    cout<<"string s="<<s<<endl;
    return 0; 
}

運(yùn)行結(jié)果:


1.PNG

※成員函數(shù)putback

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cin.putback('a');
    cout<<"請(qǐng)輸入一個(gè)ch數(shù)據(jù):";
    cin>>ch;
    cout<<"string ch="<<ch<<endl;
    return 0; 
}


運(yùn)行結(jié)果:

3.PNG

※成員函數(shù)peek

#include<iostream>
using namespace std;
int main()
{
    int i;
    string s;
    //輸入緩沖區(qū)中第一個(gè)字符 
    cout<<"strart"<<endl;
    char ch=cin.peek();//沒有數(shù)據(jù)的時(shí)候,等待用戶輸入 
    cout<<"end"<<endl;
    if((ch>='0')&&(ch<='9'))
    {
        cin>>i;//"123abc"   i=123
        cout<<"int i="<<i<<endl;
    }
    else
    {
        cin>>s;
        cout<<"string s="<<s<<endl;
    }
}


運(yùn)行結(jié)果:


2.PNG

**※整數(shù)流的基數(shù):流操縱算子dec、oct、hex和setbase **

**整數(shù)通常被解釋為十進(jìn)制(基數(shù)為10)整數(shù)。如下方法可改變流中整數(shù)的基數(shù): **

  • 插人流操縱算子hex可設(shè)置十六進(jìn)制基數(shù)(基數(shù)為16)、
  • 插人流操縱算子oct可設(shè)置八進(jìn)制基數(shù)(基數(shù)為8)、
  • 插人流操縱算子dec可恢復(fù)十進(jìn)制基數(shù)。
    **用流操縱算子setbase來改變基數(shù),使用setbase或其他任何參數(shù)化的操縱算子都必須在程序中包含頭文件iomanip
    如果不明確地改變流的基數(shù),流的基數(shù)是不變的 **
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int i=11;
    cout<<hex<<i<<" "<<dec<<i<<endl;//11轉(zhuǎn)換為16進(jìn)制變成b,在轉(zhuǎn)換成十進(jìn)制變成11
    cout<<setbase(8)<<i<<endl;//變成八進(jìn)制
    return 0; 
}

運(yùn)行結(jié)果:

1.PNG

※設(shè)置浮點(diǎn)數(shù)精度(precision、setprecision)

可以用流操縱算子setprecision或成員函數(shù)percision控制小數(shù)點(diǎn)后面的位數(shù)。

  • 設(shè)置了精度以后,該精度對(duì)之后所有的輸出操作都有效,直到下一次設(shè)置精度為止。
  • 無參數(shù)的成員函數(shù)percision返回當(dāng)前設(shè)置的精度。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double roof2=3.141592666;
    for(int places=0;places<=9;places++)
    cout<<setprecision(places)<<roof2<<'\\n' ;
    return 0; 
}

運(yùn)行結(jié)果:


2.PNG

※設(shè)置域?qū)?setw、width)

  • 成員函數(shù)ios.width設(shè)置當(dāng)前的域?qū)?即輸入輸出的字符數(shù))并返回以前設(shè)置的域?qū)挕?/li>
  • 如果顯示數(shù)據(jù)所需的寬度比設(shè)置的域?qū)捫?,空位用填充字符填充?/li>
  • 如果顯示數(shù)據(jù)所需的寬度比設(shè)置的域?qū)挻?顯示數(shù)據(jù)并不會(huì)被截?cái)啵到y(tǒng)會(huì)輸出所有位。
  • 域?qū)捲O(shè)置僅對(duì)下一行流讀取或流插入操作有效,在一次操作完成之后,城寬又被置回0
  • 未對(duì)所處理的輸出數(shù)據(jù)提供足夠的域?qū)挄r(shí),輸出數(shù)據(jù)將按需要的域?qū)掃M(jìn)行輸出,有可能產(chǎn)生難以閱讀的輸出結(jié)果。
例題1:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int t1[5]={12345,1,3333,33,7777};
    int t2[5]={12,4,67,54645,653};
    for(int i=0;i<5;i++)
    {
        cout.width(6);//設(shè)置寬度 類型,作用相當(dāng)于\\t
        cout<<t1[i];
        
    }
    cout<<endl;
    for(int j=0;j<5;j++)
    {
        cout.width(6);
        cout<<t2[j];
        
    }
    cout<<endl;
}

例題1:
運(yùn)行結(jié)果:

3.PNG
例題2:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    char n[30]={0};
    cin.width(5);
    while(cin>>n)
    {
        cout<<"n="<<n<<endl;
        cin.width(5);
        
    }
    return 0;
}

例題2:
運(yùn)行結(jié)果:


1.PNG

※用戶自定義的流操縱算子

#include<iostream>
#include<iomanip>
using namespace std;
ostream& tab(ostream& output)//作用相當(dāng)于\\t
{
    return output<<'\\t';
}
int main()
{
    cout<<'a'<<tab<<'b'<<'\\t'<<'c'<<endl;
    return 0;
}

運(yùn)行結(jié)果:

output.PNG

※cin.good

#include<iostream>
#include<iomanip>
using namespace std;
#include<limits>
int main()
{
    int a;
    int b;
    cin>>a;
    cout<<"a="<<a<<endl;
    cout<<"cin1="<<cin.good()<<endl;//檢查cin是否損壞 
    if(!cin.good())
    {
        cin.clear();//修復(fù)cin 
        cin.sync();//清除輸入緩存區(qū)內(nèi)的所有數(shù)據(jù) Windows下的 
    //  cin.ignore(numeric_limits<streamsize>::max(),'\\n');清除輸入緩存區(qū)內(nèi)的所有數(shù)據(jù) Linus下的 
    }
    cout<<"cin2="<<cin.good()<<endl;
    cin>>b;
    cout<<"b="<<b<<endl;
    return 0;
}

運(yùn)行結(jié)果:


4.PNG

文件

文件打開
  • 使用構(gòu)造函數(shù)打開
    創(chuàng)建流對(duì)象時(shí)直接打開文件
    eg: ofstream ofile(const char *filename, openmode);
  • 使用成員函數(shù)open打開文件
    eg: ofstream ofile;
    ofile.open(const char *filename, openmode)
  • 文件類的默認(rèn)打開方式
    打開文件時(shí),沒有指定打開模式時(shí),使用默認(rèn)的打開方式;
    ofstream: ios::out | ios::trunc
    ifstream: ios::in
    fstream: ios::in | ios::out
    對(duì)于ifstream 的流對(duì)象在打開文件時(shí)即使指定的模式中沒有顯示的標(biāo)明ios::in 模式,ios::in 標(biāo)識(shí)也一直存在
    對(duì)于ofstream 的流對(duì)象打開的文件即使指定的模式中沒有顯示的標(biāo)明ios::out 模式,ios:;out 標(biāo)識(shí)也是一直存在的
    ---------------------------------------------------------------------------------------------------- openmode 文件打開模式
  • ios::in 輸入(讀)模式打開文件
  • ios::out 輸出(寫)模式打開文件
  • ios::app 追加模式打開文件
  • ios::trunc 若文件已經(jīng)存在則清空文件的模式打開文件
  • ios::binary 二進(jìn)制方式打開文件
    這些標(biāo)識(shí)可以單獨(dú)使用,也可以組合使用,中間用”或“ 運(yùn)算符 ”|“ 間隔。
    fstream file;
    file.open(“example”, ios::out | ios::app | ios::binary);

文件關(guān)閉

當(dāng)文件的讀寫操作完成之后,我們必須將文件關(guān)閉以使文件重新變?yōu)榭稍L問的。關(guān)閉文件時(shí)需要調(diào)用成員函數(shù)close( ),它負(fù)責(zé)將緩存中的數(shù)據(jù)排放出來并關(guān)閉文件。
這個(gè)函數(shù)一旦被調(diào)用,原來的流對(duì)象就可以被用來打開其他的文件了,這個(gè)文件也可以重新被其他的進(jìn)程訪問了。

include<fstream>

fstream file;
file.open(“example”, ios::out | ios::app | ios::binary);
if(file !=NULL) {
cout<<“open failed”<<endl;
}
//……. 文件操作
file.close();

在文件中輸入
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("zyz1");//打開文件
    ofile<<"pear"<<" "<<4.5;//在文件中輸入
    ofile.close();//關(guān)閉文件
    return 0; 
} 

讀文件內(nèi)容:
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ifstream ifile("zyz1");
    char sztext[20];
    double price;
    ifile>>sztext>>price;
    cout<<sztext<<" "<<price;
    ifile.close();
    return 0; 
} 

運(yùn)行結(jié)果:


5.PNG

以二進(jìn)制的形式在文件中存儲(chǔ)

文件write
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("zyz2.txt",ios::out|ios::binary);
    char temp[20]="nihao";
    ofile.write(temp,20);
    ofile.close();
    return 0; 
} 

文件read
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ifstream ifile("zyz2.txt",ios::in|ios::binary);
    char temp[20];
    ifile.read(temp,20);
    cout<<temp<<endl;
    ifile.close();
    return 0; 
} 

運(yùn)行結(jié)果:


1.PNG
流指針相關(guān)函數(shù)
  • tellg( )和 tellp( )
返回一個(gè)pos_type類型,即整數(shù),分別代表當(dāng)前讀指針(get) 和 寫指針(put) 的位置
  • seekg( pos_type position ) 和 seekp( pos_type position )
    流指針被改變?yōu)橹赶蛭募_始計(jì)算的一個(gè)絕對(duì)位置,傳入的參數(shù)類型與函數(shù)tellg 和 tellp 的返回值類型相同
  • seekg( offset, seekdir) 和 seekp( offset, seekdir)
    從由參數(shù)seekdir 設(shè)定的位置開始計(jì)算一個(gè)位移 offset,其中seekdir的值可以是: ios::beg(流開始的位置),ios::cur(流當(dāng)前的位置),ios::end(流末尾的位置)
#include<iostream>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("exam1.txt",ios::out|ios::binary);
    char temp[20]="nihao";
    ofile.write(temp,20);
    ofile.close();
    
    ifstream ifile("exam1.txt");
    if(NULL==ifile)
    {
        cout<<"打開文件失敗"<<endl;
        return -1;
    }
    //定位函數(shù) -get 指針(讀指針) 
    ifile.seekg(0,ios::end);
    //指針位置函數(shù) -get指針(讀指針) 
    cout<<"get point position:"<<ifile.tellg()<<endl;
    ifile.close();
    return 0; 
} 

運(yùn)行結(jié)果:

2.PNG
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • C/C++輸入輸出流總結(jié) 前兩天寫C++實(shí)習(xí)作業(yè),突然發(fā)現(xiàn)I/O是那么的陌生,打了好長(zhǎng)時(shí)間的文件都沒有打開,今天終...
    LuckTime閱讀 1,810評(píng)論 0 6
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • C++基礎(chǔ) IO和文件操作 標(biāo)準(zhǔn)輸入輸出的設(shè)備名有哪些 文件輸入類的類名是:iostream 輸出流注意事項(xiàng)計(jì)算順...
    I踏雪尋梅閱讀 700評(píng)論 0 1
  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy閱讀 9,688評(píng)論 1 51
  • 第一章 計(jì)算機(jī)與C++編程簡(jiǎn)介 C++程序6個(gè)階段編程 ->預(yù)處理->編譯->連接->裝入->執(zhí)行1.程序在編譯器...
    rogertan30閱讀 4,516評(píng)論 0 1

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