一,C++输入和输出的概述
1)流和缓冲区
流是程序和源流或流目标之间的桥梁
磁盘驱动器以512字节(或更多)的块为单位传输信息,程序通常每次只能处理一个字节信息。所以缓冲区用来匹配这两种不同的信息传输速率。
输出时,先填满缓冲区,然后把整块数据传输给硬盘,并清空缓冲区,以备下一批输出使用。
2)isotream类管理细节
cin 对象对应于标准输入流,关联到标准输入设备。wcin 对应 wchar_t
cout 标准输出流,wcout 对应 wchar_t
cerr 标准错误流,没有缓冲直接发送给屏幕,而不会等到缓冲区填满或新的换行符,wcerr对应 wchar_t
clog 标准错误流,有缓冲区,wclog对应 wchar_t
二,使用cout进行输出
1)重载<< 操作符 : ostream & operator<<(int);
C++指向字符串存储位置的指针来表示字符串
#include <iostream> using namespace std; int main() { char *pn="tianshuai"; char name[10]="tianshuai"; cout<<pn<<endl; cout<<name<<endl; cout<<"nihao"<<endl; return 0; }
输出地址:cout<<&pn<<endl; cout<<(void *)pn<<endl; //不知道为什么输出不一样呢?
2)拼接输出: cout<<"tianshuai"<<V5<<endl; //cout<<"tianshuai" 返回cout对象
3)其他ostream方法:
cout.put('W'); //每次输出一个字符类型 字符
cout.put(65); //输出 a
char *pn = "tianshuai";
cout.write(pn,4); //输出 tian
4)刷新输出缓冲区
换行符 "\n" endl flush 刷新输出缓冲区。
5)用cout进行格式化
修改显示时使用的技术系统:
cout<<dec; //显示整数 采用 十进制 dec(cout);
cout<<hex; //显示整数 采用 十六进制
cout<<oct; //显示整数 采用 八进制
调整字段宽度:
cout.width(12); //只影响接下来显示的第一个项目,然后恢复默认。且放到12字符最右端(右对齐)
cout<<”#"; //输出为:" #";
注意:C++永远不会截断数据,假如在宽度为2的字段中打印一个7位值,C++将扩充字段
填充字符:
cout.fill("*"); //填充空白字符为 * 。影响整个程序输出
设置精度:
注意:C++默认精度为6位(末尾的0将不显示)
cout.precision(2); //精度设置为2 例如:2.153 输出为2.1
打印末尾的0和小数点:
cout.setf(ios_base::showpoint); //显示末尾0和小数点 ,不要忘记精度默认为6 例如:2.00 显示为2.00000
setf详解
注意:只有当基数为10时才使用加号。C++将十六进制和八进制都视为无符号的
cout.setf(ios_base::internal,ios_base::adjustfield); //符号或基数左对齐,值右对齐
cout.setf(ios_base::right,ios_base::adjustfield); //右对齐
iomanip头文件
cout<<setw(5)<<setfill('*')<<setprecision(3)<<endl; //设置宽度为5,填充为 * , 精度为4(小数点后4位)
cout.width(5)
cout.fill("*");
三,使用cin进行输入
1)cin>>hex; // 限定输入的进制 oct 八进制 dec 十进制
#include <iostream> using namespace std; int main() { int value; cin>>hex>>value;//输入十六进制 cout<<value<<endl; return 0; }输入:19 输出:25
2)cin检查输入
cin>>value; //输入12d cout<<value; //输出12
其中字符 d 留在输入流,接下来的cin语句从这里读取
#include <iostream> using namespace std; int main() { int value; cin>>value;//输入十六进制 string s; cin>>s; cout<<value<<endl; cout<<s<<endl; return 0; }
3)流状态
cin或 cout包含一个描述流状态的数据成员,从ios_base继承而来,由三个ios_base元素组成:eofbit、badbit或failbit。
eofbit:如果到达末尾则设置为1
badbit:流被破坏设置为1;文件读取错误
failbit:读到了非预期的字符或输出非预期字符设置为1
1>clear() 清除三个状态位
clear(eofbit)将状态设置为 eofbit,其他的被清除
2>setstate(eofbit) 只影响设置的位
4)I/O和异常
#include <iostream> #include <exception> using namespace std; int main() { cin.exceptions(ios_base::failbit);//设置状态位 cout<<"Enter numbers"<<endl; int sum=0;//统计输入数据总和 int input; try { while(cin>>input) { sum+=input; } } catch(ios_base::failure &bf)//抓设定的错误 { cout<<bf.what()<<endl;//输出错误信息 cout<<"!!!error!!!"<<endl; } cout<<"Last value entered = "<<input<<endl; cout<<"Sum = "<<sum<<endl; return 0; }
5)流状态的影响
如果输入中出现错误,则程序自动退出。
添加:
if(cin.fail()&&!cin.eof())
{
cin.clear(); //清空状态位
while(!isspace(cin.get()))//去除非法输入
{
continue;
}
}
else
cout<<"I can't go on!"<<endl;
则跳过非法输入,使得输入正常进行。
#include <iostream> #include <exception> using namespace std; int main() { //cin.exceptions(ios_base::failbit);//设置状态位 cout<<"Enter numbers"<<endl; int sum=0;//统计输入数据总和 int input; while(cin>>input) { sum+=input; } if(cin.fail()&&!cin.eof()) { cin.clear(); //清空状态位 while(!isspace(cin.get()))//去除非法输入 { continue; } } else cout<<"I can't go on!"<<endl; while(cin>>input) { sum+=input; } cout<<"Last value entered = "<<input<<endl; cout<<"Sum = "<<sum<<endl; return 0; }
6)单字符的输入
char c=cin.get(); //返回 int
cin.get(c); //返回 cin
不会跳过空格、换行符等
7)字符串输入
getline(); //抽取并丢弃输入流中的换行符
get(); //换行符留在输入流
get(char *,int ,char); // 要读入的字符串,个数,结束符
get(char *,int) //换行符默认为结束符
getline(char *, int ,char)
getline(char *,int)
例如:char line[50]; cin.get(line,50);
cin.ignore(int ,char ) //读取并丢弃接下来的255个字符,或者到结束符为止
8)其他istream方法
1>char gross[20];
read(gross,20); //不会在读取字符后面加上 ‘\0’ 因此不能讲输入转化为字符串
2>peak();//下一个要读取的字符
while((ch=cin.peak())!='.'&&ch!='\n')
cin.get(great_input[i++]);
3>cin.putback(ch) //将字符ch插入到输入流
#include <iostream> #include <exception> using namespace std; int main() { char ch; while(cin.get(ch)) { if(ch!='#') { cout<<ch; } else { cin.putback(ch);//读取到 '#'则回退 将'#'插入输入流 break; } } if(!cin.eof()) { cin.get(ch); cout<<'\n'<<ch<<"is next input character"<<endl; } else { cout<<"end of file reached"<<endl; exit(0); } while(cin.peek()!='#') { cin.get(ch); cout<<ch; } if(!cin.eof()) { cin.get(ch); cout<<endl<<ch<<"is next input character"<<endl; } else { cout<<"end of file reached"<<endl; exit(0); } return 0; }
四,文件输入和输出
1)文件的写入和读取
#include <iostream> #include <fstream> using namespace std; int main() { ofstream fout("test.txt"); fout<<"My name is tianshuai"; fout.close(); //这里必须关闭,才能让下面读取 ifstream fin; fin.open("test.txt"); char ch; fin>>ch; //读取第一个字符 cout<<ch<<endl; char buff[80]; fin.getline(buff,80); //fin.read(buff,80); cout<<buff<<endl; fin.closr(); }
fin.open("test.txt");
if(!fin.is_open()) //文件没有被打开
3)命令行处理技术
int main(int argc , char *argv[]) //argc 为命令行中参数的个数 argv[] 存储每个命令行参数
例如:wc report1 report2 report3
则:argc=4; argv[0]=wc
4)文件模式
ifstream fin("test.txt",model); //model 默认 ios_base::in 打开文件以读取
ofstream fout;
fout.open("test.txt",model); //model 默认 ios_base::out | ios_base::trunc 打开以读取,并截短文件
ios_base::app //以追加的方式写文件
ios_base::binary //二进制模式打开
5)inline void eatline() //该方法放在读取文件之后,如果没有完全读完,继续读
{
while(cin.get()!='\n')
continue;
}
#include <iostream>
#include <fstream>
using namespace std;
const char *filename = "test.bat";
int main()
{
ofstream fout(filename,ios_base::binary | ios_base::app |ios_base::out);
if(!fout.is_open())
{
cerr<<"Con't open"<<filename<<"file for output";
exit(0);
}
fout<<"My name is tianshuai";
fout.close(); //这里必须关闭,才能让下面读取
ifstream fin;
fin.open(filename,ios_base::in|ios_base::binary);
char ch;
fin>>ch; //读取第一个字符
cout<<ch<<endl;
char buff[80];
fin.getline(buff,80);
//fin.read(buff,80);
cout<<buff<<endl;
fin.close();
}