走进C++程序世界------IO标准库介绍

流概述

   流是C++标准的组成部分,流的主要目标是,将从磁盘读取文件或将输入写入控制台屏幕的问题封装起来,创建流后程序猿就能够使用它。流将负责处理全部的细节。

IO类库

在C++输入、输出操作是通过C++系统提供的完毕I/O操作的一组类实现的。

主要包括:

标准流类:处理与标准输入设备(键盘)和输出设备(显示器)关联的数据流
文件流类:处理与磁盘文件关联的数据流
字符串流类:利用内存中的字符数组处理数据的输入输出

异常类等:处理异常错误.


标准IO对象:
     包括iostream类的C++程序启动时。将创建并初始化4个对象。
     1、cin :处理来自标准输入的设备(键盘)的输入。
      2、cout:处理到标准输出设备(控制台屏幕)的输出
      3、cerr: 处理到标准错误设备(控制台屏幕)的非缓冲输出,因为这是非缓冲的。所以发送到cerr的不论什么数据都被马上写入标准错误设备。而不等缓冲区填满或者刷新指令
      4、clog:处理输出到标准错误设备(控制台屏幕)的缓冲错误信息。
     注意:cin 在遇到空格或者换行符时标示输入结束。
/*File : cerr.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>

using namespace std;

/*格式化输出
 *输出缓冲遇到换行,有输入、满、程序结果、flush会刷新
 */


int main()
{
	char ch;
	cerr << "World    "; /*无缓冲。不可重定向*/
	cin >> ch;
	clog << "Clog    ";  /*理论上“缓冲,不可重定向”实际上无缓冲*/
	cout << "Hello    "; /*缓冲,可重定向*/


	 //输入随意字符退出
	cin >> ch;
	return 0;
}

看输出:

问题:从上面的输出结果。貌似cout也不是带缓冲的,不知道是我理解的问题,还是代码写的有错误,求解?

 以下是一些基本函数的使用方法
/*File : get.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>

using namespace std;

int main()
{
	char ch;
	while((ch = cin.get()) != EOF ){
		if (ch == '$'){
			break;
		}
		cout <<"ch = " << ch << endl;
	}

	cout << "Done" << endl;
	cin.ignore(10,'
');

	char s[25];
	cin.get(s,25);
	cout << s << endl;
	return 0;
}

输出结果:

/*File : cerr.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>
#include <string>

using namespace std;

/*i.get() 从键盘输入一个字符
 *o.put() 往屏幕输出一个字符
 *i.getline() 从键盘输入一串字符串
 *i.putback() 插入字符。
 *i.peek() //查看输入缓冲区的第一个字符
 */

void get_put()
{
	cout << "####请输入四个字符或者数字###" << endl;
	int n = cin.get();
	char c,d,e;
	cin.get(c).get(d).get(e);// istream& get(char& ch);

	cout << "n = " <<n << endl;
	cout << "c = " <<c << endl;
	cout << "d = " <<d << endl;
	cout << "e = " <<e << endl;

	cout << "还有一种输出方法" <<endl;
	cout.put(n).put(c).put(d).put(e);
	//忽略前200个字符除非遇到字符'
',而停止忽略字符
	cin.ignore(200,'
');
	char ch;
	cin >> ch;
	cout << "ch = " <<ch << endl;
	cin.ignore(200,'
');
}
void mygetline()
{
	char buf[10]={''};
	cout << "####start getline ###" <<endl;
	cout << "cin.getline 输入字符串。长度不要超过9个,'' 10个" <<endl;
	cin.clear();
	
	if(!cin.getline(buf,sizeof(buf))){
		cout << "行输入过长,错误" <<endl;
		cin.clear();
		/*清除输入过长的部分,应该不会超过1000个吧!*/
		cin.ignore(1000,'
');
	}

	string s;
	cout << "使用getline 全局函数输入字符串:" <<endl;
	getline(cin,s);//getline(cin,s,'s')
	cout << "buf = " << buf<<endl;
	cout << "s = " << s << endl;
	cout << "printf 输出s :" <<endl;
	printf("%s
",s.c_str());
	cout << "####end getline ###" <<endl;
}


void mypeek()
{
	char buf[10];
	char c;

	cout << "请输入一个字符," << endl;
	cin >>ws;//屏蔽掉空格
	c = cin.get();

	cin.putback(c);
	if(cin.peek()> '0' && cin.peek() < '9'){
		
		double d;
		cout <<"请输入一个浮点数" << endl;
		cin >> d;
		cout << "d = " << d << endl;
	}else{
		string s;
		cout <<"请输入一串字符串" << endl;
		cin >> s ;
		cout <<"s = " << s << endl;
	}

}
int main2()
{
	
	//get_put();

	//mygetline();

	//mypeek();
	return 0;
}

文件的输入和输出:
   以下介绍打开和关闭文件的流对象, ifstream ofstream
/*File : file.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>
#include <fstream>

using namespace std;

char filename[80];

/*打开文件进行读写*/
void fout_fin()
{
	
	char buffer[255];
	cout << "请输入文件名称字:" << endl;
	cin >> filename;

	ofstream fout(filename);// open for writing
	fout << "####start######" << endl;
	cout << "输入一字符串写入文件里" << endl;
	cin.ignore(1,'
');//重新启动新行(将刚才输入的文件名称忽略掉)。写入文件里,
	cin.getline(buffer,255);
	fout <<buffer <<endl;
	fout << "####end####" << endl;
	fout.close();

	ifstream fin(filename);
	cout << "###### 输入文件的内容" << endl;
	while(fin.getline(buffer,255)){
		cout <<buffer<<endl;
	}
	fin.close();
}


	/*打开文件的默认行为
	 * ios::app: 附加到已有文件的末尾,而不是删除其内容
	 * ios::ate: 跳到文件末尾,但能够在文件的人和地方写入数据
	 * ios::truc: 默认值,删除已有文件的内容
	 * ios::binary: 以二进制方式打开文件。缺省的方式是文本方式。

两种方式的差别见前文 * ios::in : 以输入方式打开 * ios::out: 以输出方式打开 */ /*在文件末尾追加内容*/ void append_file() { ofstream fout; char buff[256]; fout.open(filename,ios::app); if(!fout){ cout << "打开文件失败。" << endl; return; } cout << "追加一字符串写入文件里" <<endl; cin.getline(buff,256); fout <<"#### start append file ####" <<endl; fout << buff << endl; fout <<"#### end append file ####" <<endl; fout.close(); ifstream fin(filename); if(!fin){ cout <<"打开文件失败。

" <<endl; return; } cout << "###### 输入文件的内容" << endl; while(fin.getline(buff,256)){ cout <<buff<<endl; } fin.close(); } int main() { fout_fin(); append_file(); return 0; }


输出:

 3、strstream ostrstream istrstream

    控制字符串字符的输入和输出流。

/*File : strstream.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>
#include <strstream>

using namespace std;

/*istrstream类用于运行C风格的串流的输入操作,也就是以字符串数组作为输入设备。
 *ostrstream类用于运行C风格的串流的输出操作,也就是一字符串数组作为输出设备。
 *strstream类同一时候能够支持C风格的串流的输入输出操作。
 *
 *利用istrstream类创建类对象,制定流输入设备为字符串数组,
 *通过它向一个字符型对象输入数据
 */
void in_str()
{
	char *name = "my name is sjin";
	int size = strlen(name)+1;

	istrstream istr(name,size);
	//strstream istr(name,size,ios::in);
	
	char temp[100];
	istr.getline(temp,100);
	cout << temp << endl;
}

/*类ostrstream用于运行串流的输出
 */

void out_str()
{
	char *buf = new char[10];
	char *ptemp = "sjin";
	ostrstream ostr(buf,10,ios::out);

	//使用ostrstream输出到流对象的时候,用ends结束字符串
	ostr<<ptemp<<ends;
	cout << buf <<endl;
	delete[] buf;
	
}
int main()
{
	in_str();

	out_str();

	return 0;
}


4、stringstream

/*File : sstream.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void convert_type()
{
	stringstream sstr;
	int a = 100;
	//
	sstr<< a;
	cout <<"a :"<<a<<" 转换成字符串:"<< sstr.str() << endl;
	
	sstr.clear();//why can not clear??

	string name= "colinguan";
	char cname[200];
	sstr << name;
	sstr>>cname;
	cout << cname;

}

int main()
{
	stringstream ostr("sjin123123123123123123123123123123123");

	ostr.put('d');
	ostr.put('e');
	ostr << " love me";
	cout << ostr.str() << endl;
	char buff[1024];
	ostr.getline(buff,1024);
	cout <<buff << endl;

	convert_type();

	return 0

输出:



5 io-state 输入输出的状态

   C++中负责的输入/输出的系统包括了关于每个输入/输出操作的结果的记录信息。

这些当前的状态信息被包括在io_state类型的对象中。io_state是一个枚举类型(就像open_mode一样),以下便是它包括的值。

 
 goodbit 无错误  
 Eofbit 已到达文件尾  
 failbit 非致命的输入/输出错误。可挽回   badbit 致命的输入/输出错误,无法挽回  
有两种方法能够获得输入/输出的状态信息。

一种方法是通过调用rdstate()函数:


还有一种方法则是使用以下不论什么一个函数来检測对应的输入/输出状态:  bool bad();bool eof();bool fail();bool good(); 

/*File : io_state.cpp
 *Auth : sjin
 *Date : 20140506
 *Mail : 413977243@qq.com
 */

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void io_state()
{
	int a;

	cin >> a;
	cout <<cin.rdstate() <<endl;

	if(cin.rdstate() == ios::goodbit){
		cout << "rdstate输入数据的类型正确,无错误" <<endl;
	}

	if(cin.rdstate() == ios_base::failbit){
		cout <<"rdstate输入数据类型错误。非致命错误,可清除输入缓冲区" <<endl;
	}
	cin.clear();
	cin.clear(ios::goodbit);
}

void good_fail()
{
	int a;

	cin >> a;
	cout <<cin.rdstate() <<endl;

	if(cin.good()){
		cout << "good_fail输入数据的类型正确,无错误" <<endl;
	}

	if(cin.fail()){
		cout <<"good_fail输入数据类型错误,非致命错误,可清除输入缓冲区" <<endl;
	}
	
}

int main()
{
	cout << "输入正确类型 11" << endl;
	io_state();
	cout << "输入正确类型 C" << endl;
	io_state();

	
	//还有一种方法则是使用以下不论什么一个函数来检測对应的输入/输出状态:  bool bad();bool eof();bool fail();bool good();
	cout << "输入正确类型 11" << endl;
	good_fail();
	cout << "输入正确类型 C" << endl;
	good_fail();
}



參考资料:

1、C++ 标准IO库知识整理

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10739090.html