C++輸入輸出流包含在頭文件<iostream>中,
流的定義如下:
通過設備驅動程序與鍵盤、屏幕、文件、打印機等進行交互, iostream 類提供與之交互的方法。
輸出流:
輸出流的對象是字節目標,三個重要的輸出流類是ostream、ofstream和ostringsream。
Ostream派生於basic_ostream支持預定義的流對象又:
cout標准輸出
cerr標准錯誤輸出,不經過緩沖
clog類似cerr,使用緩沖
注:緩沖是指將所有輸出集中存放,然后一次性顯示在屏幕上,避免多次刷屏。
格式控制
輸出寬度:
輸出寬度可以采用<iostream>中自帶的width()函數,或者使用< iomanip >中的setw, setw 和寬度均不截斷值。
使用width()函數代碼如下:
1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 5 6 int _tmain(int argc, _TCHAR* argv[]) 7 { 8 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 9 for (int i = 0; i < 4; i++) 10 { 11 cout.width(10); 12 cout << values[i] << '\n'; 13 } 14 getchar(); 15 return 0; 16 }
使用setw()函數
1 #include "stdafx.h" 2 #include <iostream> 3 #include <iomanip> 4 using namespace std; 5 6 int _tmain(int argc, _TCHAR* argv[]) 7 { 8 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 9 for (int i = 0; i < 4; i++) 10 { 11 //cout.width(10); 12 cout << setw(10) << values[i] << '\n'; 13 } 14 getchar(); 15 return 0; 16 }
程序運行結果:
寬度設置
設置寬度后,cout默認空白填充,如果需要填充某個字符,可采用fill()或setfill()函數。
采用fill()函數
1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 5 6 int _tmain(int argc, _TCHAR* argv[]) 7 { 8 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 9 for (int i = 0; i < 4; i++) 10 { 11 cout.width(10); 12 cout.fill('*'); 13 cout << values[i] << '\n'; 14 } 15 getchar(); 16 return 0; 17 }
采用setfill()函數
1 #include "stdafx.h" 2 #include <iomanip> 3 #include <iostream> 4 using namespace std; 5 6 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 10 for (int i = 0; i < 4; i++) 11 { 12 cout.width(10); 13 14 cout << setfill('*') << values[i] << '\n'; 15 } 16 getchar(); 17 return 0; 18 }
程序運行結果:
精度設置
浮點的默認精度默認為六,如果需要修改,使用setprecision()。數字輸出可以設置為固定型和科學型,輸出形式采用setiosflags(ios::fixed)控制,fixed表示固定型,scientific表示科學型,默認為科學型。
科學型代碼:
1 #include "stdafx.h" 2 3 #include <iostream> 4 using namespace std; 5 6 int _tmain(int argc, _TCHAR* argv[]) 7 { 8 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 9 for (int i = 0; i < 4; i++) 10 cout << setprecision(1) 11 << values[i] 12 << endl; 13 getchar(); 14 return 0; 15 }
運行結果:
使用固定記數法
1 #include "stdafx.h" 2 #include <iomanip> 3 #include <iostream> 4 using namespace std; 5 6 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 10 for (int i = 0; i < 4; i++) 11 cout << setiosflags(ios::fixed) << setprecision(1) 12 << values[i] 13 << endl; 14 getchar(); 15 return 0; 16 }
運行結果:
將整形數字按照不同進制輸出:
1 #include "stdafx.h" 2 3 #include <iostream> 4 using namespace std; 5 6 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 10 cout << 3536 << endl;//十進制 11 cout <<dec<< 3536 << endl;//十進制 12 cout << oct << 3536 << endl;//八進制 13 cout << hex << 3536 << endl;//十六進制 14 15 getchar(); 16 return 0; 17 }
運行結果:
輸入輸出數據到文件:
1 #include "stdafx.h" 2 #include <iostream> 3 #include <fstream> 4 5 using namespace std; 6 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 ifstream ifile; 11 char buff[5] = { 0 }; 12 ifile.open("d:/FILE1.txt", ios::in); 13 ifile.getline(buff, 5); 14 // Do some output 15 ifile.close(); // FILE1 closed 16 17 cout << buff << endl;// 18 19 getchar(); 20 return 0; 21 }
運行結果:
采用>>運算符讀入整個字符串:
1 #include "stdafx.h" 2 #include <iostream> 3 #include <fstream> 4 5 using namespace std; 6 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 ifstream ifile; 11 char buff[5] = { 0 }; 12 ifile.open("d:/FILE1.txt", ios::in); 13 ifile >> buff; 14 // Do some output 15 ifile.close(); // FILE1 closed 16 17 cout << buff << endl;// 18 19 getchar(); 20 return 0; 21 }
運行結果:
寫入文件主要采用以下函數:
cout.flush() //刷新緩沖區
cout.put() //把字符寫入流中
cout.write() //將字符串寫入當前輸出流中
代碼如下:
1 #include "stdafx.h" 2 #include <iostream> 3 #include <fstream> 4 5 using namespace std; 6 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 ofstream ofile; 11 char buff[5] = { 0 }; 12 ofile.open("d:/FILE1.txt", ios::in); 13 if (!ofile) 14 { 15 cout << "打開文件失敗" << endl; 16 } 17 ofile << "123" << endl; 18 ofile.write("xyz", 3); 19 ofile.put('M'); 20 ofile.flush();//清空緩沖區 21 ofile.close(); // FILE1 closed 22 23 getchar(); 24 return 0; 25 }
運行結果: