zoukankan      html  css  js  c++  java
  • -----------------------------------A Tour of C++ Chapter8-------------------------------------------

    --------------------------Chapter8 I/O流------------------------

    1、I/O流提供了文本和数值的输入输出功能,这种输入输出是带缓冲的,可以是格式化的,也可以是未格式化的。

    2、ostream对象将有类型的对象转换为一个字符(字节)流;istream对象将一个字符(字节)转换为有类型的对象。

    3、可以用getline()来读取一整行(包括结束的换行符)。

       例:

    void hello_line()
    {
        cout << "Please input your name
    ";
        string str;
        getline(cin, str);
        cout << "Hello, " << str << "!
    ";
    }

    4、用户自定类型的I/O

      例:先自定义一个简单类型

    struct Entry
    {
        string name;
        int number;
    }
    

    运算符重载:

    ostream& operator<<(ostream& os, const Entry& e)
    {
        return os << "{"" << e.name << "","  << e.number << "}";
    }
    

     一个用户自定义的输出运算符接受它的输出流(的引用)作为第一个参数,输出完毕后,返回此流的引用。

    5、文件流:在fstream中,标准库提供了从文件读取数据以及向文件写入数据的流

    • ifstream用于从文件读取数据。
    • ofstream用于向文件写入数据。
    • fstream用于读写文件

    6、字符串流:在<sstream>中,标准库提供了从string读取数据以及向string写入数据的流:

    • istringstream用于从string读取数据。
    • ostringstream用于向string写入数据。
    • stringstream用于读写string。

    例:

    void test()
    {
        ostringstream oss;
        oss << "{temprature," << scientific << 123.456789 << "}";
        cout << oss.str() << endl;

    istringstream中的内容可以通过调用str()成员函数来获取。

  • 相关阅读:
    NGINX新手小白快速上手Centos搭建Nginx服务器
    Nginx+SSL+gunicorn+gevent+Django的配置
    nginxhttpflvmodule 的部署
    gunicorn+Django加载静态文件
    正确利用 ADO.NET
    五种提高 SQL 性能的方法
    关于能够触发BFC特性的属性,以及它们各自带来的副作用
    PID库与PID基本优化(四)
    PID库与PID基本优化(二)
    Mahony姿态解算算法笔记(一)
  • 原文地址:https://www.cnblogs.com/shuxiangguo/p/5668290.html
Copyright © 2011-2022 走看看