副标题#e#
输出主要由重载的左移操纵符(<<)来完成,输入主要由重载的右移 操纵符(>>)完成。
>>a暗示将数据放入a工具中。
<<a暗示将a工具中存储的数据拿出。
接下来我们继承看一 下C++气势气魄的串流节制,C++引入了ostringstream、istringstream、 stringstream这三个类,要利用他们建设工具就必需包括sstream.h头文件。
istringstream类用于执行C++气势气魄的串流的输入操纵。
stringstream类同时可以支持C++气势气魄的串流的输入输出操纵。
strstream类同时可以支持C气势气魄的串流的输入输出操纵。
istringstream类是从istream(输入流类)和stringstreambase(c++字 符串流基类)派生而来,ostringstream是从ostream(输出流类)和 stringstreambase(c++字符串流基类)派生而来,stringstream则是从 iostream(输入输出流类)和和stringstreambase(c++字符串流基类)派生而 来。
istringstream是由一个string工具结构而来,istringstream类从 一个string工具读取字符。
istringstream的结构函数原形如下:
istringstream::istringstream(string str);
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
istringstream istr;
istr.str ("1 56.7");
//上述两个进程可以简朴写成 istringstream istr("1 56.7");
cout << istr.str() <<endl;
int a;
float b;
istr>>a;
cout<<a<<endl;
istr>>b;
cout<<b<<endl;
system("pause");
}
#p#副标题#e#
上例中,结构字符串流的时候,空格会成为字符串参数的内部门界, 例子中对a,b工具的输入"赋值"操纵证明白这一点,字符串的空格成 为了整型数据与浮点型数据的解析点,操作分界获取的要领我们事实上完成了字 符串到整型工具与浮点型工具的拆分转换进程。
str()成员函数的利用 可以让istringstream工具返回一个string字符串(譬喻本例中的输出操纵 (cout<<istr.str();)。
ostringstream同样是由一个string 工具结构而来,ostringstream类向一个string插入字符。
ostringstream的结构函数原形如下:
ostringstream:: ostringstream(string str);
示例代码如下:#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
ostringstream ostr;
//ostr.str("abc");//假如结构的时 候配置了字符串参数,那么增长操纵的时候不会从末了开始增加,而是修改原有数 据,超出的部门
增长
ostr.put(’d’);
ostr.put (’e’); [Page]
ostr<<"fg";
string gstr = ostr.str();
cout<<gstr;
system ("pause");
}
在上例代码中,我们通过put()可能 左移操纵符可以不绝向ostr插入单个字符可能是字符串,通过str()函数返回 增长事后的完整字符串数据,但值得留意的一点是,当结构的时候工具内已经存 在字符串数据的时候,那么增长操纵的时候不会从末了开始增加,而是修改原有 数据,超出的部门增长。
对付stringstream了来说,不消我多说,各人 也已经知道它是用于C++气势气魄的字符串的输入输出的。
stringstream的构 造函数原形如下:
stringstream::stringstream(string str);
示例代码如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream ostr ("ccc");
ostr.put(’d’);
ostr.put(’e’);
ostr<<"fg";
string gstr = ostr.str();
cout<<gstr<<endl;
char a;
ostr>>a;
cout<<a
system ("pause");
}
除此而外,stringstream类的工具我 们还常用它举办string与各类内置范例数据之间的转换。
示例代码如下 :#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
stringstream sstr;
//--------int转string----- ------
int a=100;
string str;
sstr<<a;
sstr>>str;
cout<<str<<endl;
//--------string转char[]--------
sstr.clear();//假如你想通过利用同一stringstream工具实现多种范例 的转换,请留意在每一次转换之后都必需挪用clear()
成员函数。
string name = "colinguan";
char cname[200]; [Page]
sstr<<name;
sstr>>cname;
cout<<cname;
system("pause");
}
#p#分页标题#e#
接下来我们来进修一下输入/输出的状态符号的相关常识,C++中认真 的输入/输出的系统包罗了关于每一个输入/输出操纵的功效的记录信息。这些当 前的状态信息被包括在io_state范例的工具中。io_state是一个列举范例(就像 open_mode一样),以下即是它包括的值。
goodbit 无错误
Eofbit 已达到文件尾
failbit 非致命的输入/输堕落误,可挽回
badbit 致命的输入/输堕落误,无法挽回
有两种要领可以得到 输入/输出的状态信息。一种要领是通过挪用rdstate()函数,它将返回当前状 态的错误标志。譬喻,如果没有任何错误,则rdstate()会返回 goodbit.
下例示例,暗示出了rdstate()的用法:
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
if(cin.rdstate() == ios::goodbit)
{
cout<<"输入数据的范例 正确,无错误!"<<endl;
}
if(cin.rdstate() == ios_base::failbit)
{
cout<<"输入数 据范例错误,非致命错误,可排除输入缓冲区挽回!"<<endl;
}
system("pause");
}
另一种要领 则是利用下面任何一个函数来检测相应的输入/输出状态:
bool bad() ;
bool eof();
bool fail();
bool good();
下例示例,暗示出了上面各成员函数的用法:
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
if(cin.good())
{
cout<<"输入数据的范例正确,无错误! "<<endl; [Page]
}
if(cin.fail())
{
cout<<"输入数据范例错误,非致命错误,可 排除输入缓冲区挽回!"<<endl;
}
system ("pause");
}
假如错误产生,那么流状态既被标志 为错误,你必需排除这些错误状态,以使你的措施能正确适内地继承运行。要清 除错误状态,需利用clear()函数。此函数带一个参数,它是你将要设为当前 状态的符号值。,只要将ios::goodbit作为实参。
示例代码如下:
#include <iostream>
using namespace std;
int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
cin.clear (ios::goodbit);
cout<<cin.rdstate()<<endl;
system("pause");
}
凡是当我们发明输入有错 又需要纠正的时候,利用clear()变动标志为正确后,同时也需要利用get() 成员函数排除输入缓冲区,以到达反复输入的目标。
示例代码如下:
#include <iostream>
using namespace std;
int main()
{
int a;
while(1) //也可以写成for (;1;)
{
cin>>a;
if(!cin)//条 件可改写为cin.fail()
{
cout<<"输入有 错!请从头输入"<<endl;
cin.clear();
cin.get();
}
else
{
cout<<a;
break; [Page]
}
}
system("pause");
}
最后再给出一个对文件流错误标志处理惩罚的例子,固定进修,代码如下 :#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream myfile("c:\\1.txt",ios_base::in,0);
if(myfile.fail())
{
cout<<"文件 读取失败或指定文件不存在!"<<endl;
}
else
{
char ch;
while(myfile.get (ch))
{
cout<<ch;
}
if(myfile.eof())
{
cout<<"文件内容已经全部读完"<<endl;
}
while(myfile.get(ch))
{
cout<<ch;
}
}
system ("pause");
}