书:C++大学教程(第七版)
1. 处理大容量文件时,使用非格式化的I/O可以获得最好的性能。
低层次的I/O功能(也就是非格式化的I/O)制定字节应从设备传输到内存或是从内存传输到设备。这种传输通常针对单个字节。
使用非格式化的I/O可能会导致可移植性的问题。
2. <iostream>
头文件定义了cin、cout、cerr和clog对象,分别对应于标准输入流、标准输出流、无缓冲的标准错误流和有缓冲的标准错误流。同时还提供了非格式化和格式化的I/O服务。
<iomanip>
头文件中声明了对于带有参数化流运算符的格式化I/O有用的服务。
<fstream>
头文件声明了用户控制的文件处理服务。
3. basic_iostream支持输入流和输出流操作。
C++文件处理用到了类模板basic_ifstream(用于文件输入),basic_ofstream(用于文件输入)和basic_fstream(用于文件输入和输出)。
4. 使用成员函数put进行字符输出
cout.put(‘A’);
5. get和getline成员函数
// Using member functions get, put and eof.
#include <iostream>
using namespace std;
int main()
{
int character; // use int, because char cannot represent EOF
// prompt user to enter line of text
cout << "Before input, cin.eof() is " << cin.eof() << endl
<< "Enter a sentence followed by end-of-file:" << endl;
// use get to read each character; use put to display it
while ( ( character = cin.get() ) != EOF )
cout.put( character );
// display end-of-file character
cout << "\nEOF in this system is: " << character << endl;
cout << "After input of EOF, cin.eof() is " << cin.eof() << endl;
} // end main
文件结束符:Windows是Ctrl+z组合键,UNIX和MAC是Ctrl+d组合键。
single character (1)
int get();
istream& get (char& c);
c-string (2)
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);
stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
来源: http://www.cplusplus.com/reference/istream/istream/get/
成员函数ignore或者读取并丢弃一定数量的字符(默认为1个字符),或者是遇到指定分隔符时停止(默认分隔符为EOF,它使得ignore读取文件时跳过文件尾);
成员函数putback将先前使用get函数从输入流里获得的字符再放回到流中。
peek将返回输入流中的下一个字符,但并不将它从流里去除。
6. 非格式化的输入/输出使用的分别是istream与ostream的read和write成员函数。
char buffer[] = "HAPPY BIRTHDAY";
cout.write(buffer, 10); //输出buffer数组中的前10个字符,包括空字符。
任何一个空字符都会导致使用cout和<<进行输出操作的终止。
#include <iostream>
using namespace std;
int main()
{
// create two char arrays, each with 80 elements
const int SIZE = 80;
char buffer1[ SIZE ];
char buffer2[ SIZE ];
// use cin to input characters into buffer1
cout << "Enter a sentence:" << endl;
cin >> buffer1;
// display buffer1 contents
cout << "\nThe string read with cin was:" << endl
<< buffer1 << endl << endl;
// use cin.get to input characters into buffer2
cin.get( buffer2, SIZE );
// display buffer2 contents
cout << "The string read with cin.get was:" << endl
<< buffer2 << endl;
} // end main
- 设置打印的基数,hex:十六进制;oct:八进制;dec:十进制;
cout << "Enter a decimal number: ";
cin >> number; // input number
// use hex stream manipulator to show hexadecimal number
cout << number << " in hexadecimal is: " << hex
<< number << endl;
// use oct stream manipulator to show octal number
cout << dec << number << " in octal is: "
<< oct << number << endl;
// use setbase stream manipulator to show decimal number
cout << setbase( 10 ) << number << " in decimal is: "
<< number << endl;
- 使用setprecision流操纵符或ios_base的成员函数precision来控制浮点数的精度(也就是小数点的右边的位数)。调用两者之中的任何一个都可以改变输出精度,这将影响后面所有的输出操作,直到下一个设置精度操作被调用为止。
double root2 = sqrt( 2.0 ); // calculate square root of 2
int places; // precision, vary from 0-9
cout << "Square root of 2 with precisions 0-9." << endl
<< "Precision set by ios_base member function "
<< "precision:" << endl;
cout << fixed; // use fixed point format
// display square root using ios_base function precision
for ( places = 0; places <= 9; places++ )
{
cout.precision( places );
cout << root2 << endl;
} // end for
cout << "\nPrecision set by stream manipulator "
<< "setprecision:" << endl;
// set precision for each digit, then display square root
for ( places = 0; places <= 9; places++ )
cout << setprecision( places ) << root2 << endl;
- width(基类是ios_base)可以设置域宽(也就是输出值所占的字符位数或是可以输出的最大字符数)并且返回原先的域宽。宽度设置只适用于下一次输入或输出,不是黏性的。
int widthValue = 4;
char sentence[ 10 ];
cout << "Enter a sentence:" << endl;
cin.width( 5 ); // input only 5 characters from sentence
// set field width, then display characters based on that width
while ( cin >> sentence )
{
cout.width( widthValue++ );
cout << sentence << endl;
cin.width( 5 ); // input 5 more characters from sentence
} // end while
- 流操作符showpoint强制要求浮点数的输出必须带小数点和尾数零。
cout<<showpoint<<"After using showpoint "<< endl <<"9.9900 prints as: "<< 9.9900 << endl;
- 对齐(left,right,internal)
left是域左对齐并在其右边填充字符,right是右对齐并在其左边填充字符。填充字符有fill成员函数或是参数化流操纵符setfill指定。
internal表示数字的符号(或是使用showbase流操作符显示的基数)应当左对齐,同事数字的数值部分应右对齐,而中间的部分则使用填充字符填充。
int x = 12345;
// display x right justified (default)
cout << "Default is right justified:" << endl
<< setw( 10 ) << x;
// use left manipulator to display x left justified
cout << "\n\nUse std::left to left justify x:\n"
<< left << setw( 10 ) << x;
// use right manipulator to display x right justified
cout << "\n\nUse std::right to right justify x:\n"
<< right << setw( 10 ) << x << endl;
cout << internal << showpos << setw( 10 ) << 123 << endl;
//输出:+ 123
- 当遇到new操作错误时,应该抛出bad_alloc异常(在头文件中定义);可以try主动捕获。另一种方法是使用函数set_new_handler(原型在标准头文件中)。