magento做預(yù)訂類網(wǎng)站免費(fèi)做網(wǎng)站的平臺(tái)
介紹
C++中的輸入輸出流主要包括標(biāo)準(zhǔn)輸入輸出流、文件輸入輸出流和內(nèi)存數(shù)據(jù)流。
- 標(biāo)準(zhǔn)輸入輸出流可以通過使用
cin
和cout
進(jìn)行數(shù)據(jù)的讀取和輸出 - 文件輸入輸出流可以通過使用
ifstream
和ofstream
對(duì)文件進(jìn)行讀寫操作 - 內(nèi)存數(shù)據(jù)流可以通過使用
stringstream
對(duì)字符串進(jìn)行讀寫操作
應(yīng)用舉例
- 標(biāo)準(zhǔn)輸入輸出流:
#include <iostream>
using namespace std;int main() {int num;cout << "請(qǐng)輸入一個(gè)整數(shù):";cin >> num;cout << "您輸入的整數(shù)是:" << num << endl;return 0;
}
- 文件輸入輸出流:
#include <fstream>
using namespace std;int main() {ifstream fin("input.txt"); // 打開文件 input.txt,讀取其中的數(shù)據(jù)int num;fin >> num; // 從文件中讀取一個(gè)整數(shù)fin.close(); // 關(guān)閉文件 input.txtcout << "您輸入的整數(shù)是:" << num << endl; // 輸出讀取到的整數(shù)return 0;
}
- 內(nèi)存數(shù)據(jù)流:
#include <sstream>
using namespace std;int main() {stringstream ss; // 創(chuàng)建一個(gè)內(nèi)存字符串流 ss,用于讀寫字符串?dāng)?shù)據(jù)int num = 123; // 一個(gè)整數(shù)數(shù)據(jù)ss << num; // 將整數(shù)數(shù)據(jù)寫入內(nèi)存字符串流 ss 中string str = "abc"; // 一個(gè)字符串?dāng)?shù)據(jù)ss << str; // 將字符串?dāng)?shù)據(jù)寫入內(nèi)存字符串流 ss 中int num2; // 讀取整數(shù)數(shù)據(jù)到變量 num2 中ss >> num2; // 從內(nèi)存字符串流 ss 中讀取整數(shù)數(shù)據(jù)到變量 num2 中cout << "您輸入的整數(shù)是:" << num2 << endl; // 輸出讀取到的整數(shù)數(shù)據(jù)到屏幕上return 0;
}