日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

STL6-输入输出流

發(fā)布時間:2025/3/15 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL6-输入输出流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

cout 是 console output 縮寫

程序 和鍵盤 之間有一個輸入緩沖區(qū)

程序 和 顯示器 之間有一個輸出緩沖區(qū)

?

#include<iostream> #include<windows.h> #include<iomanip> using namespace std; #if 0 cout << "dd"; //全局流對象,默認和顯示器關聯(lián) cin; cerr; //標準錯誤 沒有緩沖區(qū) 輸出數(shù)據(jù)到顯示器 clog; //標準日志 有緩沖區(qū) 輸入數(shù)據(jù)到顯示器 #endif//標準輸入流 cin.get()從緩沖區(qū)取數(shù)據(jù),若是沒有則阻塞 void test01() {//char ch;//while ((ch = cin.get())!=EOF) { //EOF結(jié)束符:Ctrl+Z // cout << ch << endl;//}//cin.getchar ch2;//cin.get(ch2); //讀取一個字符char buf[256] = { 0 };//cin.get(buf, 256); //從緩沖區(qū)讀一個字符串cin.getline(buf, 256); //獲得一行數(shù)據(jù)不會讀取換行符,\n之前的數(shù)據(jù)cout << buf; }//cin.ignore() 忽略當前字符 void test02() {char ch;cin.get(ch); 從緩沖區(qū)讀數(shù)據(jù), 為空阻塞cout << ch << endl;cin.ignore(); //忽略當前字符,從緩沖區(qū)取走cin.ignore(2); //忽略幾個字符cin.ignore(10, '\n'); //若前十個都沒有字符都沒有\(zhòng)n,則忽略十個,只有在10個之前發(fā)現(xiàn)\n,則都不忽略cin.get(ch);cout << ch << endl; }//cin.peek 查看緩沖區(qū)第一個字符 void test03() {cout << "please input array or string:" << endl;char ch;ch=cin.peek(); //偷窺一下緩沖區(qū),返回第一個字符if(ch >= '0' && ch <= '9'){int number;cin >> number;cout << "input number" << number << endl;}else {char buf[256] = { 0 };cin >> buf;cout << "the string is:" << buf << endl;} }//cin.putback將數(shù)據(jù)拿回到緩沖區(qū) cin.get從緩沖區(qū)拿走數(shù)據(jù) void test04() {cout << "please input string or number:" << endl;char ch;cin.get(ch); //從緩沖區(qū)取走一個字符if (ch >= '0' && ch <= '9') {//ch放回到緩沖區(qū)cin.putback(ch);int number;cin >> number;cout << "input the number" << number << endl;}else {cin.putback(ch);char buf[256] = { 0 };cin >> buf; //將buf存入輸入流中cout << "input the string"<<buf << endl;} }//標準輸出流 //endl 有兩個作用:1.換行 2.刷新緩存區(qū) //不加endl大多數(shù)情況下,也能正常輸出, //是因為在系統(tǒng)較為空閑時候,會查看緩存區(qū)的內(nèi)容,如果發(fā)現(xiàn)新的內(nèi)容,便進行輸出。 void test05() {cout << "hello world"<<endl;cout.flush();cout.put('h').put('e').put('l') << endl;cout.write("zhaosi",strlen("zhaosi")); }//格式化輸出 void test06() {int number = 10;cout << "十進制" << endl;cout << number << endl;//1、成員方法的方式cout.unsetf(ios::dec); //卸載當前默認的10進制輸出方式cout.setf(ios::oct); //八進制輸出cout.setf(ios::showbase);cout << "八進制" << endl;cout << number << endl;cout.unsetf(ios::oct);cout.setf(ios::hex);cout << "十六進制" << endl;cout << number << endl;cout << "輸出控制寬度" << endl;cout.width(10);cout.fill('*');cout.setf(ios::left); //左右對齊cout << number << endl;//2、通過控制符int number2 = 10;cout << hex << setiosflags(ios::showbase)<< setw(10) //寬度<< setfill('~')<<setiosflags(ios::right)<< number2 << endl; } int main(){test06();return 0; }

#include<iostream> using namespace std; #include<fstream> //文件讀寫 //文本文件讀寫 void test01() {//ifstream ism;//ism.open("source.txt", ios::in);ifstream ism("source.txt",ios::in); //只讀方式打開文件,文件流(管道)//ofstream osm("target.txt", ios::out); //清除后復制ofstream osm("target.txt", ios::out | ios::app); //以追加的方式復制if (!ism) { //!肯定重載cout << "open file fail" << endl;return;}//讀文件char ch;while (ism.get(ch)) {cout << ch;osm.put(ch); //往流里增加數(shù)據(jù)}//關閉文件ism.close();osm.close(); }//二進制文件操作 對象序列化 class Person { public:Person(){}Person(int age,int id):age(age),id(id){}void show() {cout << "Age:" << age << "id:" << id << endl;} public:int age;int id; };void test02() {//文本模式讀的是文本文件嗎? 都是二進制方式存儲windows:\r\n-->\n linux:\n-->\n//二進制模式讀的是二進制模式嗎? 是Person p1(10, 20), p2(30, 40); //p1和p2是以二進制的方式存在程序中//把p1 和 p2寫進文件中ofstream osm("target對象.txt",ios::out | ios::binary); osm.write((char*)&p1, sizeof(Person)); //二進制方式寫文件osm.write((char*)&p2, sizeof(Person));osm.close();ifstream ism("target對象.txt", ios::in | ios::binary);Person p3,p4;ism.read((char*)&p3, sizeof(Person));ism.read((char*)&p4, sizeof(Person));p3.show();p4.show();ism.close(); }int main() {test02();return 0; }

總結(jié)

以上是生活随笔為你收集整理的STL6-输入输出流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。