1.cin、cin.get()、cin.getline()
cin: 输入一系列字符,其中自动略过空格、tab、换行符等空白符。遇到文件结束符时返回0,但并不把这个0值放到输入对象中。
cin.get(): 接收一个输入字符(包括空白符),返回该字符值,遇到文件结束符时,返回EOF(即这个文件结束符)。
2.peek、putback、ignore
ignore
#include <iostream.h>
int main()
{
char buf[30];
cout<<"enter a sentence: \n";
cin.ignore();
cin.getline(buf,30);
cout<<buf<<endl;
return 0;
}
输入输出情况:
enter a sentence:
rtt
tt
去掉了输入流中第一个字符。
putback
#include <iostream.h>
#include <ctype.h>
int main()
{
char c,buf[30];
int i=0;
cout<<"enter a sentence: \n";
c=cin.get();
if( isdigit(c))
cin.putback(c);
cin.getline(buf,30);
cout<<buf<<endl;
return 0;
}
输入输出情况:
1)
enter a sentence:
rtt
tt
2)
enter a sentence:
6tt
6tt
可见,当输入第一个是数字时将压回输入流,由getline再接收。但putback好像只能处理最后一个get的字符,而不能用于get在while循环体中的情况。Msdn说,如果压回去的字符不是刚刚从get获得的,而是其他字符时,结果将是未定义的。但初步验证,压回其他字符时也能正确的用get再取。
Peek
接收一个字符,并把多余输入除换行符外都显示出来
#include <iostream.h>
#include <ctype.h>
int main()
{
char c,p;
cout<<"enter a sentence: \n";
cin>>c;
cout<<c;
while(cin.peek() != '\n')
cout<<(p=cin.get());
return 0;
}
enter a sentence:
i am happy
i am happy
read
接收指定数目的字符到数组中(可读入空白符),提前结束的话输入文件结束符。
gcount
统计刚才用read读入的字符数目。
#include <iostream.h>
#include <ctype.h>
int main()
{
const int SIZE = 80;
char buf[SIZE];
cout<<"Enter a sentence:\n";
cin.read(buf,20);
cout<<"The sentence entered was :\n";
cout.write(buf,cin.gcount());
cout<<endl;
return 0;
}
Write
从数组中输出指定数目的字符,若这个数目大于数组长度,则输出整个数组。
#include <iostream.h>
#include <ctype.h>
int main()
{
cout.write("abc",10);
cout<<endl;
return 0;
}
输出:
abc
整数的基数:
Dec(十进制)
Oct(八进制)
Hex(十六进制
设置基数的函数 setbase(int n) ,n可以是10、8、或16。
如果不明确的改变流的基数,流的基数是不变的。
设置浮点数精度:
setprecision (必须带参数)
precision (无参数时返回当前设置的精度)
设置后,对以后的输出都有效。
#include <iostream.h>
#include <ctype.h>
#include <iomanip.h>
#include <math.h>
int main()
{
double root2=sqrt(2.0);
int place;
cout<<setiosflags(ios ::fixed)
<<"Square root of 2 with precisions 0-9\n"
<<"precision se by the "
<<"precision member function:"<<endl;
for(place=0; place<=9; place++)
{
cout.precision(place);
cout<<root2<<endl;
}
cout<<"precision now is: "<<cout.precision()<<endl;
for(place=0; place<=9; place++)
{
cout<<setprecision(place)<<root2<<endl;
}
// cout<<"precision now is: "<<setprecision()<<endl;
return 0;
}
设置域宽
如果所需的宽度比设置的域宽小,空位用填充字符填充。如果显示数据所需的宽度比设置的域宽大,系统输出所有位。域宽设置仅对下一行流读入或流插入抄做有效,在一次操作完后被置0。
Setw
Width:输入操作提取字符串的最大宽度比定义的域宽小1,这是因为在输入的字符串后面必须加上一个空字符。
#include <iostream.h>
#include <ctype.h>
#include <iomanip.h>
int main()
{
int w=4;
char str[10];
cout<<"Enter a sentence:\n";
cin.width(5); //每次只接收4个字符,其他的放在流中等待接收。
while( cin>> str )
{
cout.width(w++); //将4个字符输出,设置每次输出的域宽增加1
cout<<str<<endl; //输出字符
cin.width(5); //设置接收4个字符
}
return 0;
}
输入输出情况:
Enter a sentence:
happy new year
happ
y
new
year
^Z
先输出happ(四个字符),y留在下一次。遇到空格接收结束,第二次只有y,到y输出时,输出域宽是5。下一个接收的是new(后面的空格断开了接收),……
对齐(ios::left,ios::right,ios::internal)
默认是右对齐
Internal: 标示一个数的符号位左对齐,数值右对齐。
设置left、right、internal时,setf的第二个参数必须是ios::adjustfield
设置填充字符( fill、setfill)
空格是默认的填充字符。Fill函数返回以前设置的填充字符。
整数流的基数( ios::dec ios::oct ios::hex ios::showbase)
Showbase强制输出整数值的基数。ios::uppercase 决定是0x还是0X
#include <iostream.h>
#include <iomanip.h>
int main()
{
int x=100;
cout<<setiosflags(ios::showbase | ios::uppercase)
<<x<<'\n'
<<oct<<x<<'\n'
<<hex<<x<<endl;
return 0;
}
浮点数和科学记数法( ios::scientific ios::fixed)
secientific标志使浮点数按科学记数法输出。
fixed标志使浮点数按照定点格式输出,显示小数点及后面的位数(位数的指定由precision决定,若没设置,则按默认-小数后6位)。
设置及清除格式标志( flags setiosflags resetiosflags)
无参数的flags只返回格式标志的当前设置(long型)。带一个long参数的flags按参数的指定格式设置标志,返回以前的标志设置。
cout的setf成员函数有两种写法:
1. cout.setf(long)
2. cout.setf(long a, long b)
若a是ios::left ios::right ios::internal 那么b对应ios::adjustfield
若a是ios::oct ios::hex ios::dec b对应 ios::basefield
若a是ios::scientific ios::fixed b对应 ios::floatfield
错误流状态
当流中发生格式错误时,设置failbit
当发生导致数据丢失错误时,设置badbit
如果eofbit、failbit、badbit都没有设置,则设置goodbit
成员函数rdstate返回流的错误状态。
成员函数clear把一个流的状态恢复为“好”,如:
cin.clear()
清除cin,并为流设置goodbit。