《C++Primer4》附录-标准库io库的使用
来源:互联网 发布:av淘宝最新地址获取 编辑:程序博客网 时间:2024/06/05 03:17
1、输出为bool类型格式为true和false,需要使用cout<< boolalpha, 取消bool字母格式输出使用 cout<<noboolalpha,
2、将整型int i = 10,按各种进制输出的格式如下:
八进制输出: cout<<oct<<i <<endl , 十六进制输出: cout<<hex<<i <<endl; 十进制输出: cout<<dec<< i <<endl;(十进制为默认输出)
如果输出需要带上标记(0, 0x),需要声明为 cout << showbase << hex << i <<endl; cout<< noshowbase <<endl; // 0xa
如果标记大写,则 写为 cout<<uppercase << hex << i << endl << nouppercase <<endl;
3、 cout .precision() 返回当前精度, cout.setpricision(12) 将当前精度设置为12位(整个浮点型数字不包括小数点)。 需要包含 #include<iomanip> 头文件。
4、采用科学计数法输出,需要声明为 cout<< scientific , 对齐则声明为 cout<<fixed, 取消这些格式还原默认的格式输出 需要调用 cout.unsetf(ostream::floatfield);
5、强制显示小数点 声明为 cout << showpointer << i; // 默认精度为6, 取消显示小数点格式: cout << noshowpointer << i << endl;
6、填充输出用法:
#include <iomanip>#include <iostream>using namespace std;void set_fill_usage(){int i = -16;double d = 3.14159;//默认右边对齐cout<<"i: "<<setw(12)<<i<<"next col"<<endl;cout<<"d: "<<setw(12)<<d<<"next col"<<endl;//靠左边对齐cout<<left;cout<<"i: "<<setw(12)<<i<<"next col"<<endl;cout<<"d: "<<setw(12)<<d<<"next col"<<endl;//靠右边对齐cout<<right;cout<<"i: "<<setw(12)<<i<<"next col"<<endl;cout<<"d: "<<setw(12)<<d<<"next col"<<endl;//负数符号靠左,数字靠右cout<<internal;cout<<"i: "<<setw(12)<<i<<"next col"<<endl;cout<<"d: "<<setw(12)<<d<<"next col"<<endl;//空格位置以#填充cout<<setfill('#');cout<<"i: "<<setw(12)<<i<<"next col"<<endl;cout<<"d: "<<setw(12)<<d<<"next col"<<endl;//恢复以空格填充cout<<setfill(' ');}
7、默认情况下,输入操作符忽略空白,制表符,换行符,进纸和回车, 如果想要不忽略这些,需要声明cin<<noskipws, 恢复默认的忽略声明为 cin << skipws。
void skipws_usage(){char ch;cin >> noskipws;while (cin >> ch){if (ch != '#'){cout<< ch;}elsebreak;}cin>> skipws;}
8、利用seekg,seekp,tellg等函数访问文件copyOut.txt,将文件中每行最后的位置输出到最后一行
abcd
efg
hi
j
代码:
int fstream_access_file_usage(){fstream inOut("copyOut.txt", fstream::ate | fstream::in | fstream:: out); //fstream::ate means fixed at the end of the fileif (!inOut){cerr<<"ERROR! Unable to open file" <<endl;return EXIT_FAILURE;}ifstream::pos_type end_mark = inOut.tellg();inOut.seekg(0, fstream::beg);int cnt = 0;string line;while (inOut && inOut.tellg() != end_mark && getline(inOut, line)){cnt+=line.size()+1;ifstream::pos_type mark = inOut.tellg();inOut.seekp(0, fstream::end);inOut<< cnt;if (mark != end_mark){inOut<< " ";}inOut.seekg(mark);}inOut.clear();inOut.seekp(0, fstream::end);inOut<<"\n";return 0;}
测试效果:
abcd
efg
hi
j
5 9 12 14
- 《C++Primer4》附录-标准库io库的使用
- c标准IO库使用
- 【C++】标准IO库
- C++primer 标准IO库
- C++Primer 标准IO库
- C++_Primer_chapter8 标准IO库
- 附录A 参考手册 和 附录B 标准库
- 标准C的IO
- C标准库IO函数的错误识别
- C标准库:以字节为单位的IO函数
- C标准库:以串为单位的IO函数
- 《C++Primer》读书笔记——附录A.2 标准库算法概览
- 《C++Primer》读书笔记——附录A.3 标准库随机数
- 标准 IO 库的缓冲区
- 《C++primer》 函数 标准IO库
- IO之标准C库buffer
- c++Primer,八,标准IO库
- C标准库:IO错误处理
- ZOJ 1951
- 递归函数的解题思路
- WEB2.0技术
- iOS开发(4)UITextField
- JAVA中获取当前系统时间
- 《C++Primer4》附录-标准库io库的使用
- ZOJ 1188
- c++数据结构内存对齐方式
- ZOJ 1392
- 编译libvpx
- ZOJ 3775 ?(>_o)!(模拟)
- ZOJ 1970
- Mysql 优化
- 【互联网高手教你如何搜集你想要的信息】