柚子快報邀請碼778899分享:c++ 14-1、IO流
柚子快報邀請碼778899分享:c++ 14-1、IO流
14-1、IO流
lO流打開和關(guān)閉lO流打開模式lO流對象的狀態(tài)
非格式化IO二進(jìn)制IO讀取二進(jìn)制數(shù)據(jù)獲取讀長度寫入二進(jìn)制數(shù)據(jù)
讀寫指針 和 隨機(jī)訪問設(shè)置讀/寫指針位置獲取讀/寫指針位置
字符串流
lO流打開和關(guān)閉
通過構(gòu)造函數(shù)打開I/O流 其中filename表示文件路徑,mode表示打開模式
打開輸入流 ifstream (const char* filename ,openmode mode)打開輸出流 ofstream(const char* filename , openmode mode);打開輸入輸出流 fstream (const char* filename , openmode mode);
lO流打開模式
ios::out 打開文件用于寫入,不存在則創(chuàng)建,存在則清空 適用于ofstream(缺省)/fstreamios::app 打開文件用于追加,不存在則創(chuàng)建,存在不清空 適用于ofstream/fstreamios::trunc 打開時清空原內(nèi)容 適用于ofstream/fstreamios:in 打開文件用于讀取,不存在則失敗,存在不清空 適用于ifstream(缺省)/fstreamios::ate 打開時定位文件尾 適用于ifstream/fstreamios.:binary 以二進(jìn)制模式讀寫 適用于ifstream/ofstream/fstream
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ofstream(文件輸出流)類
int main( void ){
ofstream ofs1("./file",ios::out);
if(!ofs1){ // !(ofs1.operator bool())
cout << "ofs1流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
}
ofs1 << 1234 << ' ' << 56.78 << ' ' << "Hello" << '\n';
if(!ofs1){
cout << "ofs1流對象狀態(tài)錯誤 -- 寫文件失敗" << endl;
}
ofs1.close();
ofstream ofs2("./file",ios::app);
if(!ofs2){ // !(ofs2.operator bool())
cout << "ofs2流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
}
ofs2 << "World" << endl;;
if(!ofs2){
cout << "ofs2流對象狀態(tài)錯誤 -- 寫文件失敗" << endl;
}
ofs2.close();
return 0;
}
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ifstream(文件輸入流)類
int main( void ){
ifstream ifs1("./file",ios::in);
if(!ifs1){ // !(ifs1.operator bool())
cout << "ifs1流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
}
int i; double d; string s1,s2;
ifs1 >> i >> d >> s1 >> s2;
if(!ifs1){
cout << "ifs1流對象狀態(tài)錯誤 -- 讀文件失敗" << endl;
}
cout << i << ' ' << d << ' ' << s1 << ' ' << s2 << endl;
ifs1.close();
ifstream ifs2("./file",ios::ate);
if(!ifs2){ // !(ifs2.operator bool())
cout << "ifs2流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
}
ifs2.seekg(0,ios::beg);
int ii; double dd; string ss1,ss2;
ifs2 >> ii >> dd >> ss1 >> ss2;
if(!ifs2){
cout << "ifs2流對象狀態(tài)錯誤 -- 讀文件失敗" << endl;
}
cout << ii << ' ' << dd << ' ' << ss1 << ' ' << ss2 << endl;
ifs2.close();
return 0;
}
lO流對象的狀態(tài)
I/O流類對象內(nèi)部保存當(dāng)前狀態(tài),其值為以下常量的位或
ios:goodbit: 0,一切正常ios::badbit: 1,發(fā)生致命錯誤ios::eofbit: 2,遇到文件尾ios::failbit: 4,打開文件失敗或?qū)嶋H讀寫字節(jié)數(shù)未達(dá)預(yù)期
l/O流類對象支持到bool類型的隱式轉(zhuǎn)換
發(fā)生1,2,4等情況,返回false,否則返回true將I/O流對象直接應(yīng)用到布爾上下文中,即可實現(xiàn)轉(zhuǎn)換
處于1或4狀態(tài)的流,在復(fù)位前無法工作
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ifstream(文件輸入流)類
int main( void ){
ifstream ifs2("./file",ios::ate);
if(!ifs2){ // !(ifs2.operator bool())
cout << "ifs2流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
}
int ii; double dd; string ss1,ss2;
cout << "--------------------第一次讀數(shù)據(jù)-----------------------" << endl;
ifs2 >> ii >> dd >> ss1 >> ss2;// ifs2.operator>>(ii).operator>>(dd)>>operator>>(ss1)>>operator>>(ss2)
if(!ifs2){
cout << "ifs2流對象狀態(tài)錯誤 -- 讀文件失敗" << endl;
cout << "ifs2是0狀態(tài)嗎?" << ifs2.good() << ", ifs2是1狀態(tài)嗎?" << ifs2.bad()
<< ", ifs2是2狀態(tài)嗎?" << ifs2.eof() << ", ifs2是4狀態(tài)嗎?" << ifs2.fail() << endl;
cout << "ifs2的具體狀態(tài):" << ifs2.rdstate() << endl;
}
cout << ii << ' ' << dd << ' ' << ss1 << ' ' << ss2 << endl;
ifs2.clear();
ifs2.seekg(0,ios::beg);
cout << "--------------------第二次讀數(shù)據(jù)-----------------------" << endl;
ifs2 >> ii >> dd >> ss1 >> ss2;// ifs2.operator>>(ii).operator>>(dd)>>operator>>(ss1)>>operator>>(ss2)
if(!ifs2){
cout << "ifs2流對象狀態(tài)錯誤 -- 讀文件失敗" << endl;
cout << "ifs2是0狀態(tài)嗎?" << ifs2.good() << ", ifs2是1狀態(tài)嗎?" << ifs2.bad()
<< ", ifs2是2狀態(tài)嗎?" << ifs2.eof() << ", ifs2是4狀態(tài)嗎?" << ifs2.fail() << endl;
cout << "ifs2的具體狀態(tài):" << ifs2.rdstate() << endl;
}
cout << ii << ' ' << dd << ' ' << ss1 << ' ' << ss2 << endl;
ifs2.close();
return 0;
}
非格式化IO
寫入字符 ostream& ostream::put (char ch);一次向輸出流寫入一個字符,返回流本身刷輸出流 ostream& ostream::flush (void);將輸出流緩沖區(qū)中的數(shù)據(jù)刷到設(shè)備上,返回流本身讀取字符 int istream::get (void);成功返回讀到的字符,失敗或遇到文件尾返回EOF istream& istream::get (char& ch);返回輸入流本身,其在布爾上下文中的值,成功為true,失敗或遇到文件尾為false讀取行 istream& istream::getline (char* buffer,streamsize num, char delim = ‘\ n’);
讀取字符 (至定界符)到buffer中。一旦讀取了num個字符還未讀取定界符,第num個字符設(shè)置為 ‘\0’,返回 (輸入流對象狀態(tài)為4)。如果因為遇到定界符 (缺省為 ‘\n’ ) 返回 (輸入流對象狀態(tài)為0)定界符被讀取并丟棄,追加結(jié)尾空字符 ‘\0’,讀指針停在該定界符的下一個位置遇到文件尾,返回 (輸入流對象狀態(tài)為6)
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ofstream(文件輸出流)、ifstream(文件輸入流)類
int main( void ){
ofstream ofs("./noformat",ios::out);
if(!ofs)
cout << "ofs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
for( char c = ' '; c <= '~';c++)
ofs.put(c).flush();
ofs.close();
ifstream ifs("./noformat",ios::in);
if(!ifs)
cout << "ifs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
char c;
// 單參get
while(1){
ifs.get(c);
if(!ifs)
break;
else
cout << c;
}
cout << endl;
ifs.clear();
ifs.seekg(0,ios::beg);
// 無參get
while(1){
c = ifs.get();
if( c == EOF )
break;
else
cout << c;
}
cout << endl;
ifs.close();
return 0;
}
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ofstream(文件輸出流)、ifstream(文件輸入流)類
int main( void ){
ifstream ifs("./getline",ios::in);
if(!ifs)
cout << "ifs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
char buf[256];
while(1){
ifs.getline(buf,256,'\n');
if(!ifs)
break;
else{
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
}
}
/*
ifs.getline(buf,256,'\n'); // aa\n
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
ifs.getline(buf,256,'\n'); // bbbb\n
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
ifs.getline(buf,256,'\n'); // cccccc\n
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
ifs.getline(buf,256,'\n'); // dddddddd\n
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
ifs.getline(buf,256,'\n'); // 0123456789\n
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
ifs.getline(buf,256,'\n'); //
cout << buf << endl;
cout << "ifs流對象狀態(tài)值:" << ifs.rdstate() << endl;
*/
ifs.close();
return 0;
}
二進(jìn)制IO
讀取二進(jìn)制數(shù)據(jù)
istream& istream::read (char* buffer,streamsize num)
從輸入流中讀取num個字節(jié)到緩沖區(qū)buffer中返回流對象本身,其在布爾上下文中的值,成功(讀滿)為true,失敗(沒讀滿)為false如果沒讀滿num個字節(jié),函數(shù)就返回了,比如遇到文件尾,最后一次讀到緩沖區(qū),buffer中的字節(jié)數(shù)可以通過istream::gcount()函數(shù)獲得
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ofstream(文件輸出流)、ifstream(文件輸入流)類
int main( void ){
ofstream ofs("./binary",ios::out);
if(!ofs)
cout << "ofs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
ifstream ifs("./getline",ios::in);
if(!ifs)
cout << "ifs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
char buf[3];
while(1){
ifs.read(buf,3);
if(ifs){
ofs.write(buf,3);
// cout << buf; // 讀滿3個字符
}
else{
// 沒有讀滿3個字符
int len = ifs.gcount();
ofs.write(buf,len);
// buf[len]='\0';
// cout << buf;
break;
}
}
ifs.close();
ofs.close();
return 0;
}
獲取讀長度
streamsize istream::gcount (void)
返回最后一次從輸入流中讀取的字節(jié)數(shù)
寫入二進(jìn)制數(shù)據(jù)
ostream& ostream::write (const char* buffer,streamsize num);
將緩沖區(qū)buffer中的num個字節(jié)寫入到輸出流中返回流本身,其在布爾上下文中的值,成功(寫滿)為true,失敗(沒寫滿)為false
#include
#include
using namespace std;
// C++標(biāo)準(zhǔn)庫已經(jīng)設(shè)計好的類ofstream(文件輸出流)、ifstream(文件輸入流)類
int main( void ){
ofstream ofs("./binary",ios::out);
if(!ofs)
cout << "ofs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
ifstream ifs("./getline",ios::ate);
if(!ifs)
cout << "ifs流對象狀態(tài)錯誤 -- 打開文件失敗" << endl;
int size = ifs.tellg();
char buf[size];
ifs.seekg(0,ios::beg);
ifs.read(buf,size);
ofs.write(buf,size);
ifs.close();
ofs.close();
return 0;
}
讀寫指針 和 隨機(jī)訪問
設(shè)置讀/寫指針位置
istream& istream::seekg (off_type offset,ios::seekdir origin);
ostream& ostream::seekg (off_type offset,ios::seekdir origin);
origin表示偏移量offset的起點:
ios::beg從文件的第一個字節(jié)ios::cur從文件的當(dāng)前位置ios::end從文件最后一個字節(jié)的下一個位置 offset為負(fù)/正表示向文件頭/尾的方向偏移 讀/寫指針被移到文件頭之前或文件尾之后,則失敗
獲取讀/寫指針位置
返回讀/寫指針當(dāng)前位置相對于文件頭的字節(jié)偏移量
pos type istream::tellg (void);
pos type ostream::tellp (void):
字符串流
輸出字符串流
#include
ostringstream oss ;
oss << 1234 << ' ' << 56.78 <<' '<< "ABCD";
string os = oss.str();
輸入字符串流
#include
string is ("1234 56.78 ABCD") ;
istringstream iss (is);
int i;
double d;
string s;
iss >> i >> d >> s;
柚子快報邀請碼778899分享:c++ 14-1、IO流
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。