C++ Primer 5th笔记(1)chapter 1
1. 文件重定向
定義:指定std::cin 和std::out的輸入文件和輸出文件
用法: xx.exe < infileName >outfileName
eg:
輸入文件add_Item.input 內容為:
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
執行命令:cprimer.exe <…\data\add_Item.input >…\data\additem.output
輸出add_Item.output內容為:
0-201-78345-X 5 110 22
2. 另外一個程序
代碼如下
#include <iostream> #include "Sales_item.h"int main() {Sales_item total; // variable to hold data for the next transaction// read the first transaction and ensure that there are data to processif (std::cin >> total) {Sales_item trans; // variable to hold the running sum// read and process the remaining transactionswhile (std::cin >> trans) {// if we're still processing the same bookif (total.isbn() == trans.isbn()) total += trans; // update the running total else { // print results for the previous book std::cout << total << std::endl; total = trans; // total now refers to the next book}}std::cout << total << std::endl; // print the last transaction} else {// no input! warn the userstd::cerr << "No data?!" << std::endl;return -1; // indicate failure}return 0; }輸入為:
0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
結果為:
0-201-70353-X 4 99.96 24.99
0-201-82470-1 4 181.56 45.39
0-201-88954-4 16 198 12.375
0-399-82477-1 5 226.95 45.39
0-201-78345-X 5 110 22
3.一些概念
緩沖區:IO設施通常將輸入或輸出數據保存在一個緩沖區中。默認情況下,讀cin會刷新cout;程序非正常終止也會刷新cout。
clog:一個ostream對象,程序的標準錯誤信息
Cerr:用于輸出錯誤信息或其他不屬于程序正常邏輯的輸出內容
cout:ostream:iosteam
cin:istream:iosteam
參考
[1]: 代碼 https://github.com/thefistlei/cplusprimer/tree/main/cprimer
總結
以上是生活随笔為你收集整理的C++ Primer 5th笔记(1)chapter 1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UML类图说明
- 下一篇: C++ Primer 5th笔记(2)c