C++中的IO类(iostream, fstream, stringstream)小结

        以前学习C++的时候, 总是囫囵吞枣地理解cin, cout等东东, 最近又在复习C++,  复习到IO类这一章节的时候, 有点感触, 所以呢, 打算记录一下。

 

         俗话说, 一图胜过千言万语, 这不是没有道理的, 下面, 我们来看看基本IO类的继承结构:

 

      在我们写C++简单代码的时候, 最喜欢写#include <iostream>  , 那么, 这实际上是在包括IO流的头文件, 而用using namespace std;则表示用标准空间, 这样才能用cin,cout, endl等东东啊。

      从上图看, 实际上可以把IO类分为三类:

      1. iostream类: 负责与控制台输入输出打交道, 这个我们已经很熟悉了。   注意: 实际具体又可以区分为:istream和ostream

      2. fstream类:   负责与文件输入输出打交道, 这个我们接触过。  注意: 实际具体又可以区分为:ifstream和ofstream

      3. stringstream类:负责与string上的输入输出打交道, 这个我们暂时还真没用过。  注意: 实际具体又可以区分为:istringstream和ostringstream

 

       下面, 我们来一一学习/复习:

      1. IO类之iostream

          iostream类的对象, 如cin, cout, 会直接与控制台输入输出关联, 下面我们来看看最简单的程序:

 

#include <iostream>
using namespace std;

int main()
{
	int i = -1;
	
	cin >> i; // cin从控制台接收输入, 并保存在i中

	cout << i << endl; // count把i的值输出到控制台

	return 0;
}

 

     很简单很好理解吧。

 

     2. IO类值之fstream

      fstream的对象, 与文件建立关联, 我们已经很熟悉了, 直接看代码吧:

 

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联
	if(!in)
	{
		cout << "error" << endl;
		return 1;
	}

	string line;
	while(getline(in, line))
	{
		cout << line << endl;	
	}

	return 0;
}


    3. IO类之stringstream

 

     stringstream的对象与内存中的string对象建立关联, 往string对象写东西, 或者从string对象读取东西。

     我们先看这样一个问题, 假设test.txt的内容为:

lucy 123 

lili 234 456

tom 222 456 535

jim 2345 675 34 654

     其中每行第一个单词是姓名, 后面的数字都是他们的银行卡密码, 当然啦, jim的银行卡最多, 有4张, 现在, 要实现如下输出, 该怎么做呢?

 

lucy 123x 

lili 234x   456x 

tom 222x   456x   535x 

jim 2345x   675x   34x   654x 

     直接看程序吧:

 

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
	ifstream in("test.txt"); // 建立in与文件test.txt之间的额关联
	if(!in)
	{
		cout << "error" << endl;
		return 1;
	}

	string line; 
	string password;
	while(getline(in, line))
	{
		istringstream ss(line); // 建立ss与line之间的关联
		int i = 0;
		while(ss >> password) // ss从line读取东西并保存在password中
		{	
			cout << password + (1 == ++i ? "" : "x") << " ";
		}
		
		cout << endl;
	}

	return 0;
}

       结果ok.

       

       下面, 我们来看看如何利用istringstream实现字符串向数值的转化:

 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	int a = -1;
	string s = "101";
	istringstream is(s); // 建立关联
	cout << is.str() << endl; // 101,  看来is和s确实关联起来了啊

	is >> a;

	cout << a << endl; // 101

	return 0;
}

 


      当然, 我们也可以把数字格式化为字符串, 如下(下面这个程序有问题):

 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string str;
	ostringstream os(str); // 建立关联, 但实际没有关联上啊!!!
	os << "hello";

	cout << str << endl;  // str居然是空, 怎么不是"abc"呢? 我不理解

	return 0;
}

     看来, 上面的关联是不成功的, 改为:

 

 

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	int a = -1;
	ostringstream os;
	os << "hello" << a;

	cout << os.str() << endl; // hello-1

	return 0;
}


      所以, 我总结呢, istringstream的关联是ok的, 但是ostringstream不能依赖于关联, 不知道C++为啥要这样搞。

 

 

      好吧, 到此为止, 总算是对三种类型的IO类有所掌握了。
 

 

 

 

 

 

 

 

       

  • 14
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值