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