C++ I/O

weixin_34120274發表於2015-07-17

1 cin,>>

行動:從標準輸入裝置讀取資料,忽略空格、標籤、進入
>> :運營脫節

2 cin和 get,ignore,putback,peek

get(ch):將輸入流中全部字元,包含空白符,讀入到引數列表中指定變數相應的記憶體空間中
cin.ignore(intExp,chExp):忽略掉接下來的intExp個字元。或者首次遇到chExp曾經的全部字元
putback:將get函式在輸入流中讀取的最後一個字元回退到輸入流中
peek:檢查輸入流。而且告訴程式輸入流中下一個字元是什麼,但並不實際讀取這個字元。

演示樣例程式碼:
#include <iostream>
using namespace std;

int main() {
	char ch;
	cout << "Enter a String :" << endl;
	// 輸入:abcd

	cin.get(ch);// 取出輸入流中第一字元 ch = a,  輸入流中剩餘:bcd
	cout << "ch = " << ch << endl;
	cin.get(ch);// 取出輸入流中第一字元 ch = b,  輸入流中剩餘:cd
	cout << "ch = " << ch << endl;
	cin.putback(ch); // 將ch =b , 回退到輸入流中,輸入流中剩餘:bcd
	cin.get(ch);// 取出輸入流中第一字元 ch = b,  輸入流中剩餘:cd
	cout << "ch = " << ch << endl;
	ch = cin.peek(); // 嘗試讀取輸入流中的第一字元。單並沒有取走。輸入流中剩餘:cd
	cout << "ch = " << ch << endl;
	cin.get(ch);// 取出輸入流中第一字元 ch = c,  輸入流中剩餘:d
	cout << "ch = " << ch << endl;

	return 0;
}
輸出結果:
Enter a String :
abcd
ch = a
ch = b
ch = b
ch = c
ch = c

3 clear

當輸入流遇到錯誤是,系統將忽略掉全部使用該輸入流的I/O語句。能夠使用Clear函式將輸入流恢復到正常狀態。
演示樣例程式碼:
#include <iostream>
using namespace std;

int main() {
	int a = 23;
	int b = 34;
	cout << "Enter a number followed by a character:" << endl;
	cin >> a >> b;
	cout << "a="<< a << ",b=" << b << endl;
	cin.clear();// 將輸入流變為正常狀態。沒有此句會直接結束
	cin.ignore(200,'\n');
	cout << "Enter two numbers:" << endl;
	cin >> a >> b;
	cout << "a="<< a << ",b=" << b << endl;
	return 0;
}
執行結果:
Enter a number followed by a character:
232a
a=232,b=34
Enter two numbers:
23 21
a=23,b=21

4 setprecision,fixed,showpoint,setw

標頭檔案:#include<iomanip>
setprecision:控制輸出浮點數小數精度 ,
fixed:能夠使輸出的浮點數依照固定小數點個數輸出。取消方法 cout.unsetf(ios::fixed)
showpoint:當計算機輸出以固定小數點個數輸出小數是。假設小數點後部分的值是0,那麼預設輸出的數字將沒有小數點和小數點後面的數字。showpoint控制符能夠強制顯示輸出小數點和小數點後面的0

演示樣例程式碼:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	double x, y, z;
	x = 15.572;
	y = 1234.43;
	z = 9823.2385;


	cout << fixed << showpoint;
	cout << setprecision(2) << "setprecision(2)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;
	cout << setprecision(3) << "setprecision(3)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;
	cout << setprecision(4) << "setprecision(4)" << endl;
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
	cout << "z = " << z << endl;

	cout << setprecision(3) << x << " " << setprecision(2) << y << " " << setprecision(4) << z << endl;


	return 0;
}

執行結果:
setprecision(2)
x = 15.57
y = 1234.43
z = 9823.24
setprecision(3)
x = 15.572
y = 1234.430
z = 9823.238
setprecision(4)
x = 15.5720
y = 1234.4300
z = 9823.2385
15.572 1234.43 9823.2385

能夠用setf來指定fixed,scientific,showpoint。格式標誌:ios::fixed,ios::scientific,ios::showpoint,。ios::fixed,ios::scientific 都是C++資料型別ios::floatfield的一部分。

當指定fixed或者scientific標誌時,必須保證

ios::fixed 和ios::scientific 不能同一時候出現,並且必須保證ios::floatfield作為函式setf的第二個引數使用。

cout.setf(ios::fixed,ios::floatfield)
cout.setf(ios::showpoint)

另外還能夠:

cout << setiosflags(ios::fixed);
cout << setiosflags(ios::showpoint)
或:cout << setiosflags(ios::fixed | ios::showpoint)

setw:指定輸出表示式值佔用的列數,預設是右對齊。
演示樣例程式碼:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int a = 345;
	double y = 76.384;

	cout << fixed << showpoint;

	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << endl;
	cout << setw(5) << a << setw(5) << "Hi" << setw(5) << x << endl << endl;

	cout << setprecision(2);
	cout << setw(6) << a << setw(6) << y << setw(6) << x << endl;
	cout << setw(6) << x << setw(6) << a << setw(6) << y << endl;

	cout << setw(5) << a << x << endl;
	cout << setw(2) << a << setw(4) << x << endl;


	return 0;
}
執行結果:
12345678901234567890
   19
  345   Hi   19


   345 76.38    19
    19   345 76.38
  34519
345  19

5 fill,setfill,left,right

fill,setfill 設定沒有資料列,填充除空格符以為的其它字元
cout.fill('*');
cout << setfill('*');

演示樣例程式碼:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int y = 7624;

	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;
	cout.fill('*');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	cout << setw(5) << x << setw(7) << setfill('#') << y << setw(8) << "warm" << endl;
	cout << setw(5) << setfill('@') << x << setw(7) << setfill('#') << y << setw(8) << setfill('^')<< "warm" << endl;
	cout.fill(' ');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	
	return 0;
}
執行結果:
12345678901234567890
   19   7624    warm
***19***7624****warm
***19###7624####warm
@@@19###7624^^^^warm
   19   7624    warm

left,right 控制左對齊或者右對齊
cout << left
cout.unsetf(ios::left)

演示樣例程式碼:
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int x = 19;
	int y = 7624;

	cout << left;
	cout << "12345678901234567890" << endl;
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;
	cout.fill('*');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	cout << setw(5) << x << setw(7) << setfill('#') << y << setw(8) << "warm" << endl;
	cout << setw(5) << setfill('@') << x << setw(7) << setfill('#') << y << setw(8) << setfill('^')<< "warm" << endl;
	
	cout.unsetf(ios::left);
	cout.fill(' ');
	cout << setw(5) << x << setw(7) << y << setw(8) << "warm" << endl;

	
	return 0;
}
執行結果:
12345678901234567890
19   7624   warm
19***7624***warm****
19***7624###warm####
19@@@7624###warm^^^^
   19   7624    warm

在標準C++ 中。left和right控制符僅僅能夠作為格式標誌使用。

因此,必須以setf和 setiosflags控制符來設定left和right

6 flush

清空緩衝區

7 string I/O

從標準裝置中獲取一行字串:getline(cin,str)

8 file I/O

檔案I/O 操作的5個步驟
1,#include<fstream>
2,定義檔案流變數
3,將檔案流與輸入輸出檔案關聯
4,讀寫檔案
5。關閉檔案

fileStreamVariable.open(sourceName,fileOpeningMode)

開啟方式:
ios::in                                 以輸入方式開啟。ifstream型別預設開啟方式
ios::out                               以輸出方式開啟。ofstream型別預設開啟方式
ios::nocreate                     假設檔案不存在,則開啟操作失敗
ios::app                              假設檔案存在,則從檔案的末尾開始寫入資料。假設不存在建立一個新檔案 
ios::ate                               以輸出方式開啟檔案將檔案讀寫位置移到檔案末尾。輸出資料能夠寫到檔案的不論什麼地方
ios::trunc                            假設檔案存在,當中的內容將被刪除(截掉)
ios::noreplace                   假設檔案存在。則開啟操作失敗

#include<fstream>
#include<stream>
using namespace std;
int main()
{
	fstream fin("data.txt");  //開啟檔案
	string ReadLine;
	while(getline(fin,ReadLine))  //逐行讀取,直到結束
	{
		...
	} 
	fin.close();
	return 0
}

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
	int test1,test2,test3,test4,test5;
	char id;
	double avg;

	ifstream ifs("test.txt");

	if (!ifs)
	{
		cout << "Cannot open input file." << endl;
		return 1;
	}

	ofstream ofs("result.txt");
	ifs >> id >> test1 >> test2 >> test3 >> test4 >> test5;

	avg = (test1 + test2 + test3 + test4 + test5) / 5.0;
	ofs << fixed << showpoint << setprecision(2);
	ofs << setw(25) << right << "Student Id:" << setw(8) << id << endl;
	ofs << setw(25) << right << "Test Score:" << setw(8) << test1 << setw(8) << test2 << setw(8) << test3 << setw(8) << test4 << setw(8) << test5 << endl;
	ofs << setw(25) << right << "Average test score:" << setw(8) << avg << endl;

	ifs.close();
	ofs.close();
}


總結:

  1. C++ 中的流是從源到目標的一系列字元的有限序列
  2. 輸入流是從源到計算機的流
  3. 輸出流是從計算機到目標的流
  4. cin代表標準輸入。是輸入流物件,通常初始化為標準輸入裝置——鍵盤
  5. cout代表標準輸出,是輸出流物件,通常初始化為標準輸出裝置——螢幕
  6. 雙目運算子>>和輸入流物件,如cin,結合使用時被稱為流析取運算子。 >> 左邊的物件必須是輸入流物件。如cin,右邊的運算元必須是變數
  7. 雙目運算子<<和輸出流物件。如cout,結合使用時被稱為流插入運算子。 << 左邊的物件必須是輸出流物件。如cout。右邊的運算元必須是變數
  8. 當使用運算子>> 將資料讀入變時。將忽略掉資料前的全部空白符
  9. 為了使用cin和cout,程式必須包括iostream檔案
  10. get 函式用於逐個讀入字元,不會忽略到不論什麼空白符
  11. ignore函式用於在一行中略掉某些資料
  12. putback函式用於將get函式讀入的最後一個字元退回到輸入流中
  13. peek函式返回輸入流中的當前字元。可是並不將該字元重輸入流中移走
  14. 試圖將非法資料讀入變數將導致輸入流進入錯誤狀態
  15. 一旦輸入流進入錯誤狀態,能夠使用clear函式將輸入流恢復成正常狀態
  16. setprecision控制符用來設定輸出浮點數的精度
  17. fixed控制符將浮點數以固定小數點個數輸出
  18. showpoint控制符用來指定浮點數必須包括小數點和小數點末尾的零
  19. setw控制資料列寬,預設右對齊
  20. 假設在setw 中指定的輸出列寬小於實際資料須要,資料不會被截短,而是依照實際寬度輸出
  21. get,ignore,put,peek,fill,clear,setf,unsetf流函式標頭檔案在 iostream中
  22. setprecision,setw,setfill,setiosflags在iomanip中
  23. 標頭檔案fstream包括ifstream和ofstream定義
  24. 開啟需要關閉該檔案

版權宣告:本文部落格原創文章,部落格,未經同意,不得轉載。

相關文章