I/O类 & 文件读写操作

来源:互联网 发布:sql语句常见面试题 编辑:程序博客网 时间:2024/05/20 06:23

这里写图片描述

fstream 和 stringstream 都是继承自类iostream

输入类ifstream、istringstream都继承自istream

输出类ofstream、ostringstream都继承自ostream;

因此,可以在istream对象上执行的操作,也可以在ifstream和istringstream上执行。其他类也有相同的情况。


fstream,文件I/O

#include <fstream>ofstream         //文件写操作 内存写入存储设备 ifstream         //文件读操作,存储设备读区到内存中fstream          //读写操作,对打开的文件可进行读写操作 

iostream控制台I/O

#include<iostream>istream         //从流中读取数据ostream         //向流中写入数据iostream        //读写流

stringstream string流

#include<sstream>istringstream   //从string读数据ostringstream   //向string中写数据stringstream    //既可读string,也可写string

可以使用istringstream**将一个大字符串拆分成若干个单个Word**;这是最常用的情况;

#include <iostream>#include<fstream>#include<sstream>using namespace std;int main(){    string s="1234  256457";    string str1;    istringstream strm(s);//将字符串读到strm中    while(strm>>str1)      //拆分出每个Word,当没有可拆分的Word会返回null,跳出循环    {        cout<<str1;    }    return 0;}