1.1 输入和输出流对象
定义:stream是一条数据流,字符序列在其中川流不息。
头文件:#include<iostream>
包含头文件后,程序将自动创建8个stream对象(4个用于窄字符,4个用于宽字符):
cin/wcin 对应标准输入stream,默认关联到标准输入设备。
cout/wcout 对应标准输出stream,默认关联到标准输出设备。
cerr/wcerr 对应标准错误stream,用于显示错误信息,默认情况下关联到标准输出设备,不过该stream没有缓冲,信息将直接发送给屏幕。
clog/wclog 对应标准错误stream,用于显示错误信息,默认情况下也关联到标准输出设备,不过该stream有缓冲。
注:“正常输出”和"错误信息输出"进行分离的主要目的是让程序以不同的方式对待两种输出,例如可以将正常输出重新定向某个文件,而同时仍然令错误信息显示于控制台。
1.2 文件流对象
定义:文件流对象用于文件操作,使用时程序需要实例化文件流类对象。
头文件:#include<fstream>
文件流类:
ofstream 按文件输出;
ifstream 按文件输入;
fstream 按文件输出和输入;
二、stream操作符
输入操作符:>>;
输出操作符:<<;
三、操控器
操控器专门用来操控stream对象,仅改变输入或者格式化输出的解释方式
endl 换行并刷新输出缓冲区
ends 输出'\0'
flush 刷新output缓冲区
ws 读入并忽略空格
四、stream状态
4.1 状态分类:
goodbit 代表一切都好
eofbit 代表文件末尾标志
failbit 代表错误,某个IO未操作成功
badbit 代表毁灭性错误,不确定状态
4.2 状态相关成员函数
4.2.1 good()
若stream无异常,返回true
4.2.2 bad()
若发生毁灭性错误时,返回true
示例代码如下:
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
bool b=cout.bad();
cout<<b<<endl;
b=cout.good();
cout<<b<<endl;
return 0;
}
4.2.3 eof()
若程序在读取文件时,遇到文件末尾end-of-file时,返回true。
示例程序代码:
#include "stdafx.h"
#include "iostream"
#include "iosfwd"
#include "fstream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
fstream fs;
int n=1;
fs.open("eof.txt");
cout<<fs.eof()<<endl;
fs>>n;
cout<<fs.eof()<<endl;
return 0;
}
4.2.4 fail()若发生错误,返回true
4.3 stream状态和异常
4.3.1 exception(flags)设置引发异常的标志
示例程序代码:
#include "stdafx.h"
#include "exception"
#include "iostream"
#include "ostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ios::iostate olde=cin.exceptions();
cout<<"old exceptions:"<<olde<<endl;
int x=0;
try{
cin.exceptions(ios::eofbit|ios::failbit|ios::badbit);
cin>>x;
}
catch(exception& e)
{
cout<<"exception: "<<e.what()<<endl;
}
return 0;
}
4.3.2 operator void*(),常用来判断是否标准输入完毕
应用举例:while(cin>>x)
此时的cin用于条件判断,cin会调用operator void*,返回stream是否发生错误
4.3.3 operator !(),使用operator!进行反向测试,返回stream是否发生错误
应用举例:if(!cin>>x)
五、标准输入和输出函数
5.1 标准输入函数:
5.1.1 >>
有限制,前面的空格会自动跳过,当在某行输入字符时,只有那些非空字符进入接收字符的变量中,前面的空格被越过,而且skipws对>>不起作用。
5.1.2 get()
int get()
返回从流中读入的字符
istream& get(char* pch,int nCount,char delim='\n')
返回流中读入的字符序列,读入的数据存在pch中,函数结束的条件是已经深入nCount-1个字符或者遇见回车换行符,或者遇到文件结束标志end-of-file。
istream& get(unsigned char* puch,int nCount,char delim='\n')
功能和第二种形式类似,只不过将数据流存在无符号型数组puch中
istream& get(signed char* psch,int nCount,char delim='\n')
功能和第二种形式类似,只不过将数据流存在有符号型数组psch中
istream& get(char& ch)
从输入流中读取单个字符,并存在rch中
istream& get(unsigned char& ruch)
从输入流中读取单个字符,存在ruch中
istream& get(singned char& rsch)
从输入流中读取单个字符,存在rsch中
istream& get(streambuf& rsb,char delim='\n')
从输入流中抽取字符序列存放到streambuf的对象中,如果遇到指定的分割符或者文件结束标志eof,抽取过程终止
5.1.3 getline()
istream& getline(char* pch,int nCount,char delim='\n')
从输入流中抽取字符序列,并保存在字符型数组pch中,若已经输入nCount个字符或者遇见回车符'\n',或者遇见文件结束标志eof结束
istream& getline(char* puch,int nCount,char delim='\n')
从输入流中抽取字符序列,并保存在无符号字符型数组puch中,若已经输入了nCount个字符或者遇见回车符'\n',或者遇见文件结束标志eof结束
istream& read(signed char* psch,int nCount)
读取nCount个字符并存在字符串psch中,返回数据流,数据流的状态可表明读取工作是否成功,读取过程中,字符串pch中的字符不会由于字符终止符号结束。必须确保字符串pch容量nCount个字符,读取过程中如果遇到eof标示会出现错误,failbit和eofbit会被设置
5.1.4 readsome()
streamsize istream::readsome(char* str,stream count)
最多读入字符数count,并存入字符串str中,函数返回值是读取的字符个数。readsme()会读取streambuffer内所有的有效字符,遇到eof不会报错,不会设置标志位eofbit,failbit
5.1.5 getcount()
返回上次“非格式化读取操作”所读入的字符个数
5.1.6 ignore()
ignore()的三种形式,作用均是提取字符,并将这些字符舍弃不用
ignore()
ignore(streamsize count)
ignore(streamsize count,int delim)
5.1.7 int_type peek()
返回输入序列中下一个被读入的字符,并不读取该字符,如果输入序列中没有数据,函数返回eof
5.1.8 unget()
将上一次读取的字符重新放回stream中,下一次从输入流中读取时,便可以讲这些字符读出来
示例代码如下:
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char chdim[7]={0,0,0,0,0,0,0};
cout<<"int cin.get():"<<endl;
cin.get(chdim,6,'\n');
cout.write(chdim,6);
cout<<endl;
cin.get();
cout<<"cin.get(char ch): "<<endl;
char ch;
cin.get(ch);
cout.put(ch);
cout<<endl;
cin.get();
cout<<"cin.getline(char* ch,nCount)"<<endl;
cin.getline(chdim,6,'\n');
cout.write(chdim,6);
cout<<endl;
char dim[20];
fstream fio;
fio.open(".\\text.txt",ios::in);
fio.read(dim,20);
cout.write(dim,20);
fio.close();
cout<<endl;
return 0;
}
5.2 标准输出函数
5.2.1 <<
把字符和字符串写到输出流对象
5.2.2 ostream& ostream::put(char c)
把单个字符c写到输出流对象
5.2.3 write()
ostream& write(const char* pch,int nCount)
ostream& write(const unsigned char* puch,int nCount)
ostream& write(const signed char* psch,int nCount)
三个函数的功能均是将字符串的nCount个字符写到输出流中,字符串终止符号不会终止写入操作,程序员必须保证至少含有nCount个字符,否则导致不可预期的行为
5.2.4 ostream& ostream::flush()
刷新stream缓冲区,将所有缓冲区数据强制写入到所属的设备或IO通道
示例代码如下:
#include "stdafx.h"
#include "fstream"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char chardim[30];
char ch[6];
fstream fio;
fio.open(".\\text.txt",ios::in|ios::out);
int cnt=10;
int gcnt=0;
istream i0=fio.read(chardim,cnt);
if(i0.good())
{
cout<<"Read succeed!"<<endl;
}
gcnt=fio.gcount();
string buf;
buf.assign(chardim,10);
cout<<"the input chars from file text.txt are:"<<buf<<endl;
cout<<"the count of the last read function: "<<gcnt<<endl;
istream is=fio.ignore(3,EOF);
buf.erase(buf.begin(),buf.end());
i0=fio.read(ch,5);
if(i0.good())
{
cout<<"Read Succeed"<<endl;
}
buf.assign(ch,5);
cout<<"secondly the input chars from test.txt are: "<<buf<<endl;
char chc=fio.peek();
if(chc==EOF)
{
cout<<"Read Succeed"<<endl;
}
else
{
cout<<"some char has existed in file."<<endl;
}
i0=fio.unget();
i0=fio.read(ch,5);
if(i0.good())
{
cout<<"Read Succeed"<<endl;
}
buf.assign(ch,5);
cout<<"Thirdly the input chars from file test.txt are: "<<buf<<endl;
fio.flush();
fio.close();
return 0;
}