您的位置:首页 > 编程语言 > C语言/C++

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

2015-11-07 21:58 585 查看
俗话说, 一图胜过千言万语, 这不是没有道理的, 下面, 我们来看看基本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, 会直接与控制台输入输出关联, 下面我们来看看最简单的程序:

[cpp] view
plaincopy





#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的对象, 与文件建立关联, 我们已经很熟悉了, 直接看代码吧:

[cpp] view
plaincopy





#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

直接看程序吧:

[cpp] view
plaincopy





#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实现字符串向数值的转化:

[cpp] view
plaincopy





#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;

}

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

[cpp] view
plaincopy





#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;

}

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

[cpp] view
plaincopy





#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类有所掌握了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: