代码改变世界

8 . IO类-标准IO、文件IO、stringIO

2018-03-19 14:18  清晨、午后  阅读(255)  评论(0编辑  收藏  举报

8.1 IO类

#include <iostream>    //标准IO头文件 

8.2 文件输入输出流 

#include <fstream>    //读写文件头文件
std::fstream    //从一个文件读 或 写 数据,但是不能同时进行,如正在写文件,则必须关闭后才可以读
std::ifstream    //从文件中读取数据
std::ofstream    //向一个给定文件写入数据

1.简单的例子

int main()
{
    std::string str;
    std::istream_iterator<std::string> in_iter;

    //读文件
    std::fstream fstrm("F:\\Temp.txt");
    if (fstrm.good())
    {
        fstrm >> str;
    }
    fstrm.close();

    //写文件
    std::ofstream fstrm("F:\\Temp.txt");
    fstrm << "2017.9.19yyy" << "\n\r";
    fstrm.close();

    system("pause");
    return 0;
}

2.复杂全面的例子:

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

int main()
{
    //写文件,注意:ofstream::app 每次写操作前均被定为到文件末尾
    ofstream ofstrm("h:\\1.txt", ofstream::out | ofstream::in | ofstream::ate);  //以读写的方式打开,且每次操作前均定位到文件末尾
    //ofstream ofstrm("e:\\1.txt",ifstream::binary);    //以二进制方式打开
    //ofstream::pos_type mark = ofstrm.tellp(); //获取当前标记位置
    ofstrm.seekp(ofstrm.beg);
    ofstrm << "vbbb l m" << endl;
    ofstrm.close();
    ////读文件,整行读取
    //  //创建一个ifstream(文件流)类型的ifstrm(只读模式),并打开e:\\1.txt文件与之关联,当给构造函数提供实参时,open会自动被调用
    //  ifstream ifstrm("e:\\1.txt",ifstream::in);  
    //      if(!ifstrm)
    //          return -1;
    //  
    //      vector<string> vecStrArr;   
    //      string strArr;
    //  
    //      while (getline(ifstrm,strArr))  //一组字符顺序流入strArr以换行符结束
    //      {
    //          vecStrArr.push_back(strArr);
    //
    //          //内存流的使用
    //          istringstream iss(strArr);
    //          ostringstream oss;
    //          string str;
    //          while (iss>>str)
    //          {
    //              oss<<str;               
    //          }
    //          cout<<oss.str()<<endl;
    //      }
    ////        for(int i = 0; i < vecStrArr.size(); i++)
    ////        {
    ////            cout<<vecStrArr.at(i)<<endl;
    ////        }
    ////        bool bIsEnd = ifstrm.eof(); //判断是否到达文杰结束位置
    //          ifstrm.close();
    //      
    ////读文件,单个字符读取
    //      ifstrm.open("e:\\1.txt",ifstream::in|ifstream::app);    
    //      //bool bIsOpen = ifstrm.is_open();  //检测文件是否打开成功,对于已经打开的文件调用open则不能检测
    //      if(!ifstrm.good())  //检测流是否处于有效状态
    //          return -1;
    //
    //      vector<string> vecStr;
    //      while ( ifstrm>>strArr) //一组字符顺序流入strArr,以空格、换行符等结束
    //      {
    //          vecStr.push_back(strArr);
    //      }
    ////        for(int i = 0; i < vecStr.size(); i++)
    ////        {
    ////            cout<<vecStr.at(i)<<endl;
    ////        }
    //      ifstrm.close();

    system("pause");
    return 0;
}
View Code

8.3 string 流 

#include <sstream>    //读写string流头文件
std::stringstream    //从string流读 或 写 数据
std::ostringstream    //向string流写数据(可以是基本类,也可以是重载<<的自定义类)
std::istringstream    //从string流读数据

string流应用于string与基本类型之间的转换

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

int main()
{
    std::ostringstream sstream;
    char *pChar = "abc";

    //其他任何的格式转为string
    sstream << "(" << pChar << ")" << 123 << "ddd" << *pChar;
    string str = sstream.str(); //str的内容:(abc)123ddda
    //CString cstr = str.c_str();  //string转为CString

    //string 转为char*
    const char *p = str.data();
    char *p1 = const_cast<char*> (str.data());

    //将string转换为cha(可选字符长度)
    char pch[3] = "";
    std::string str = "4h5j.";
    str.copy(pch, 2);

    cout << str;
    system("pause");
}