目錄
一、C語言的輸入與輸出
二、流是什么
三、C++IO流
3.1、C++標準IO流
3.2、C++文件IO流
四、stringstream的簡單介紹
一、C語言的輸入與輸出
C語言中我們用到的最頻繁的輸入輸出方式就是scanf?()與printf()。 scanf(): 從標準輸入設備(鍵盤)讀取數據,并將值存放在變量中。printf(): 將指定的文字/字符串輸出到標準輸出設備(屏幕)。 注意寬度輸出和精度輸出控制。C語言借助了相應的緩沖區來進行輸入與輸出。如下圖所示:
對輸入輸出緩沖區的理解:
- 可以屏蔽掉低級I/O的實現,低級I/O的實現依賴操作系統本身內核的實現,所以如果能夠屏 蔽這部分的差異,可以很容易寫出可移植的程序。
- 可以使用這部分的內容實現“行(hang)”讀取的行為,對于計算機而言是沒有“行”這個概念,有了這部分,就可以定義“行”的概念,然后解析緩沖區的內容,返回一個“行”。
二、流是什么
- “流”即是流動的意思,是物質從一處向另一處流動的過程,是對一種有序連續且具有方向性的數據( 其單位可以是bit,byte,packet )的抽象描述。
- C++流是指信息從外部輸入設備(如鍵盤)向計算機內部(如內存)輸入和從內存向外部輸出設備(顯示器)輸出的過程。這種輸入輸出的過程被形象的比喻為“流”。
- 它的特性是:有序連續、具有方向性
- 為了實現這種流動,C++定義了I/O標準類庫,這些每個類都稱為流/流類,用以完成某方面的功能。
三、C++IO流
C++系統實現了一個龐大的類庫,其中ios為基類,其他類都是直接或間接派生自ios類。
3.1、C++標準IO流
C++標準庫提供了4個全局流對象cin、cout、cerr、clog,使用cout進行標準輸出,即數據從內存流向控制臺(顯示器)。使用cin進行標準輸入即數據通過鍵盤輸入到程序中,同時C++標準庫還提供了cerr用來進行標準錯誤的輸出,以及clog進行日志的輸出,從上圖可以看出cout、cerr、clog是ostream類的三個不同的對象,因此這三個對象現在基本沒有區別,只是應用場景不同。
如圖:
在使用時候必須要包含文件并引入std標準命名空間。
注意:
1. cin為緩沖流。鍵盤輸入的數據保存在緩沖區中,當要提取時,是從緩沖區中拿。如果一次輸入過多,會留在那兒慢慢用,如果輸入錯了,必須在回車之前修改,如果回車鍵按下就無法挽回了。只有把輸入緩沖區中的數據取完后,才要求輸入新的數據。
2. 空格和回車都可以作為數據之間的分格符,所以多個數據可以在一行輸入,也可以分行輸入。但如果是字符型和字符串,則空格(ASCII碼為32)無法用cin輸入,字符串中也不能有空格。回車符也無法讀入。
3. cin和cout可以直接輸入和輸出內置類型數據,原因:標準庫已經將所有內置類型的輸入和輸出全部重載了。如圖:
4.? 對于自定義類型,如果要支持cin和cout的標準輸入輸出,需要對和>>進行重載。
5. istream類型對象轉換為邏輯條件判斷值
istream& operator>> (int& val);
explicit operator bool() const;
實際上我們看到使用while(cin>>i)去流中提取對象數據時,調用的是operator>>,返回值是istream類型的對象,那么這里可以做邏輯條件值,源自于istream的對象又調用了operator bool,operator bool調用時如果接收流失敗,或者有結束標志,則返回false。
6. 在線OJ中的輸入和輸出:
- 對于IO類型的算法,一般都需要循環輸入:
- 輸出:嚴格按照題目的要求進行,多一個少一個空格都不行。
連續輸入時,vs系列編譯器下在輸入ctrl+Z時結束
示例代碼:
解釋:首先我們需要了解為什么cin讀取后的返回值可以作為while循環的判斷條件,cin讀取后的返回值其實是一個istream類型,這里其實是利用返回值調用了istream類中重載的bool類型,如下:
while(cin>>ch)? ->? while (operator>>(cin, ch).operator bool())
通過將返回值轉化為bool值,進而判斷是否繼續讀取。那么是如何判斷轉化的bool值是true還是false的呢?其實IO流設置了四個標志,這四個標志用一個整型值來存儲,像位圖那樣,不同位置為0還是為1代表了某一個標志位是否被設置。標志如圖:
其中,eofbit被設置代表讀取到文件末尾,failbit被設置代表讀取過程中出現問題,但問題不大,通過重置標志位可繼續讀取,badbit被設置代表istream流出現了大問題,可能是溢出或者別的問題,當前流已經不可用了,goodbit被設置代表正常讀取。istream流中有四個方法,分別可以獲取對應標志位是否被設置,如果被設置,方法返回true,否則false。如圖:
示例代碼一:
int main()
{string str;//先看一下沒有讀取時標志位如何cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;// istream& operator>> (istream& is, string& str);//while (cin >> str)while (operator>>(cin, str).operator bool()){cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cout << str << endl; }cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;return 0;
}
效果:
解釋:從圖中看可以看出,最開始只有goodbit被設置,也只有當goodbit被設置時可以正常讀取,當我們輸入Ctrl+Z和換行時,eofbit,failbit被設置,程序因此停止。
示例代碼二:
int main()
{string str;//先看一下沒有讀取時標志位如何cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;// istream& operator>> (istream& is, string& str);//while (cin >> str)while (operator>>(cin, str).operator bool()){cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cout << str << endl; }cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;while (cin >> str){cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cout << str << endl;}return 0;
}
效果:
解釋:從上面圖中可以看出,當我們將標志位通過某種方式設置為不可讀取后,即使后面還有讀取操作,也會因為標記位被設置而無法讀取。如果我們想讓后面能夠繼續讀取就需要重置標記位。方法如下圖。
示例代碼三:
int main()
{string str;//先看一下沒有讀取時標志位如何cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;// istream& operator>> (istream& is, string& str);//while (cin >> str)while (operator>>(cin, str).operator bool()){cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cout << str << endl; }cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;// 恢復標志狀態cin.clear();while (cin >> str){cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cout << str << endl;}return 0;
}
注意:Ctrl+C和Ctrl+Z是不同的,Ctrl+C是發送信號將進程殺掉,而Ctrl+Z是設置標記位,使循環讀取結束。
7. 輸入的數據類型必須與要提取的數據類型一致,否則出錯。出錯只是在流的狀態字state中對應位置位(置1),程序繼續。
示例代碼:
int main()
{int i = 0;cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cin >> i;cout << i << endl;// 11ssscout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;
}
效果:
解釋:從圖中可以看出當讀取數據類型與需要的類型不一致時,failbit被設置,同時這里因為讀取失敗,將 i 置為了0。
解決方法:
int main()
{int i = 0;cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cin >> i;cout << i << endl;// 11ssscout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;cin >> i;cout << i << endl;if (cin.fail()){cin.clear();char ch;while (cin.get(ch)){cout << ch;}}cin >> i;cout << i << endl;cout << cin.good() << endl;cout << cin.eof() << endl;cout << cin.bad() << endl;cout << cin.fail() << endl << endl;return 0;
}
3.2、C++文件IO流
C++根據文件內容的數據格式分為二進制文件和文本文件。采用文件流對象操作文件的一般步 驟:
1. 定義一個文件流對象:
- ifstream i?le(只輸入用)
- ofstream o?le(只輸出用)
- fstream io?le(既輸入又輸出用)
2. 使用文件流對象的成員函數打開一個磁盤文件,使得文件流對象和磁盤文件之間建立聯系。
3. 使用提取和插入運算符對文件進行讀寫操作,或使用成員函數進行讀寫。
4. 關閉文件。
示例代碼一:
#include<fstream>int main()
{//兩種打開文件方式//可以在構造時打開//也可以調用open方法ofstream ofs("test.txt");//ofs.open("test.txt");//三種寫入方式ofs.put('x');ofs.write("hello\n", 6);//重載<< 這種方式好用ofs << "22222222" << endl;int x = 111;double y = 1.11;ofs << x << endl;ofs << y << endl;//關閉文件// 可以顯式調用close關閉// 也可以不管,它會自動關閉,因為C++中將其封裝為類// 它有析構,生命周期到了自動釋放//ofs.close();return 0;
}
示例代碼二:(統計二進制文件的字符個數)
int main()
{//ifstream ofs("test.cpp");ifstream ofs("D:\\360MoveData\\Users\\xjh\\Desktop\\8-10.png", ios_base::in | ios_base::binary);int n = 0;while (!ofs.eof()){char ch = ofs.get();//cout << ch;++n;}cout << n << endl;return 0;
}
示例代碼三:
struct ServerInfo
{//char _address[32];string _address;int _port;//Date為自己封裝的類Date _date;
};
解釋:上面代碼為一個結構化數據,其中有一個成員變量是string類型的,當string數據足夠大時,它的數據是存在堆上的,string本身只會存儲一個指向這塊堆空間的指針,這時寫入數據就只會寫入該指針,當我們去讀的時候就會造成非法訪問,因為寫的進程可能已經結束了,這塊堆空間已經釋放了。所以對于這種自定義類型進行文件操作時需要注意,我們要寫的是內容,而不是指針。
四、stringstream的簡單介紹
在C語言中,如果想要將一個整形變量的數據轉化為字符串格式,如何去做?
- 使用itoa()函數
- 使用sprintf()函數
示例代碼:
int main()
{int n = 123456789;char s1[32];_itoa(n, s1, 10);char s2[32];sprintf(s2, "%d", n);char s3[32];sprintf(s3, "%f", n);return 0;
}
但是兩個函數在轉化時,都得需要先給出保存結果的空間,那空間要給多大呢,就不太好界定, 而且轉化格式不匹配時,可能還會得到錯誤的結果甚至程序崩潰。
在C++中,可以使用stringstream類對象來避開此問題。在程序中如果想要使用stringstream,必須要包含頭文件。在該頭文件下,標準庫三個類:istringstream、ostringstream 和 stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。
stringstream主要可以用來:
1. 將數值類型數據格式化為字符串
int main()
{int a = 12345678;string sa;// 將一個整形變量轉化為字符串,存儲到string類對象中stringstream s;s << a;s >> sa;// clear()// 注意多次轉換時,必須使用clear將上次轉換狀態清空掉// stringstreams在轉換結尾時(即最后一個轉換后),會將其內部狀態設置為badbit// 因此下一次轉換是必須調用clear()將狀態重置為goodbit才可以轉換// 但是clear()不會將stringstreams底層字符串清空掉// s.str("");// 將stringstream底層管理string對象設置成"", // 否則多次轉換時,會將結果全部累積在底層string對象中s.str("");s.clear(); // 清空s, 不清空會轉化失敗double d = 12.34;s << d;s >> sa;string sValue;sValue = s.str(); // str()方法:返回stringsteam中管理的string類型cout << sValue << endl;return 0;
}
2. 字符串拼接
int main()
{stringstream sstream;// 將多個字符串放入 sstream 中sstream << "first" << " " << "string,";sstream << " second string";cout << "strResult is: " << sstream.str() << endl;// 清空 sstreamsstream.str("");sstream << "third string";cout << "After clear, strResult is: " << sstream.str() << endl;return 0;
}
3. 序列化和反序列化結構數據
class Date
{friend ostream& operator << (ostream& out, const Date& d);friend istream& operator >> (istream& in, Date& d);
public:Date(int year = 1, int month = 1, int day = 1):_year(year), _month(month), _day(day){}operator bool(){// 這里是隨意寫的,假設輸入_year為0,則結束if (_year == 0)return false;elsereturn true;}
private:int _year;int _month;int _day;
};istream& operator >> (istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}ostream& operator << (ostream& out, const Date& d)
{out << d._year << " " << d._month << " " << d._day;return out;
}struct ChatInfo{string _name; // 名字int _id; // idDate _date; // 時間string _msg; // 聊天信息};int main()
{// 結構信息序列化為字符串ChatInfo winfo = { "張三", 135246, { 2022, 4, 10 }, "晚上一起看電影吧"};ostringstream oss;oss << winfo._name << " " << winfo._id << " " << winfo._date << " "<< winfo._msg;string str = oss.str();cout << str << endl << endl;// 我們通過網絡這個字符串發送給對象,實際開發中,信息相對更復雜,// 一般會選用Json、xml等方式進行更好的支持// 字符串解析成結構信息ChatInfo rInfo;istringstream iss(str);iss >> rInfo._name >> rInfo._id >> rInfo._date >> rInfo._msg;cout << "-------------------------------------------------------"<< endl;cout << "姓名:" << rInfo._name << "(" << rInfo._id << ") ";cout << rInfo._date << endl;cout << rInfo._name << ":>" << rInfo._msg << endl;cout << "-------------------------------------------------------"<< endl;return 0;
}
istringstream,ostringstream使用示例代碼:
int main()
{int i = 123;Date d = { 2022, 4, 10 };ostringstream oss;oss << i << endl;oss << d << endl;string s = oss.str();cout << s << endl;//istringstream iss(s);//istringstream iss;//iss.str("100 2024 9 9");istringstream iss("100 2024 9 9");int j;Date x;iss >> j >> x;return 0;
}
注意:
1. stringstream實際是在其底層維護了一個string類型的對象用來保存結果。
2. 多次數據類型轉化時,一定要用clear()來清空,才能正確轉化,但clear()不會將stringstream底層的string對象清空。
3. 可以使用s. str("")方法將底層string對象設置為""空字符串。
4. 可以使用s.str()將讓stringstream返回其底層的string對象。
5. stringstream使用string類對象代替字符數組,可以避免緩沖區溢出的危險,而且其會對參數類型進行推演,不需要格式化控制,也不會出現格式化失敗的風險,因此使用更方便,更安全。