• 第八章 标准IO库


     

    code:

    /*
    
    第八章 标准IO库
    
    第8章 标准IO库
    8.1 面向对象的标准库
    8.2 条件状态
    8.3 输出缓冲区的管理
    8.4 文件的输入和输出
    8.5 字符串流
    小结 259
    
    
    
    第8章 标准IO库 243
    8.1 面向对象的标准库 244
    8.2 条件状态 247
    8.3 输出缓冲区的管理 249
    8.4 文件的输入和输出 251
    8.4.1 文件流对象的使用 251
    8.4.2 文件模式 254
    8.4.3 一个打开并检查输入文件的程序 256
    8.5 字符串流 257
    小结 259
    术语 259
    
    */
    
    
    //8.1 面向对象的标准库 ---------------------------------------------------------------------------------
    
    //8.2 条件状态 ---------------------------------------------------------------------------------
    
    // 当输入非数字时,输出停不下来
    #include <iostream>
    #include <vector>
    #include <string>
    #include <stdexcept>
    using namespace std;
    
    int main()
    {
      int ival;
      // read cin and test only for EOF; loop is executed even if there are other IO failures
      while(cin >> ival, !cin.eof())
      {
        if(cin.bad())
        // input stream is corrupted; bail out
          throw runtime_error("IO stream corrupted");
        if(cin.fail())
        {
          // bad input
          cerr << "bad data, try again"; // warn the user
          cin.clear(istream::failbit); // reset the stream
          continue; // get next input
        }
        // ok to process ival
        cout << ival << endl;
      }
      return 0;
    }
    
    
    //8.3 输出缓冲区的管理 ---------------------------------------------------------------------------------
    
    // ends中的null也会有输出,占一个字符
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    int main()
    {
      cout << "hi!" << flush;      // flushes the buffer; adds no data
      cout << "hi!" << ends;       // inserts a null, then flushes the buffer
      cout << "hi!" << endl;       // inserts a newline, then flushes the buffer
    }
    
    
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    int main()
    {
      cout << unitbuf << "first" << " second" << nounitbuf;
      cout << "first" << flush << " second" << flush;
      //
      cout << "hi!" << flush;      // flushes the buffer; adds no data
      cout << "hi!" << ends;       // inserts a null, then flushes the buffer
      cout << "hi!" << endl;       // inserts a newline, then flushes the buffer
    }
    
    /*
    如果仅因为缓冲区没有刷新,程序员将浪费大量的时间跟踪调试并没有执行的代码。基于这个原因,输出时应多使用 endl 而非 '
    '。
    flush是木有newline的endl。
    当输入流与输出流绑在一起时,任何读输入流的尝试都将首先刷新其输出流关联的缓冲区。
    ——可能是cin慢的原因。
    */
    
    
    //8.4 文件的输入和输出 ---------------------------------------------------------------------------------
    
    // c_str()
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    int main()
    {
      string s("x:	est.txt");
      cout << s << endl;
      cout << s.c_str() << endl;
      string st("x:\test.txt");
      cout << st << endl;  
      cout << st.c_str() << endl;
      
      const char *str = st.c_str();
      printf("%s",str);
      
      return 0;
    }
    
    
    // 复赛时绝对不要用这个,测试时用下。。
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    { 
      string si("x:\testin.txt"); // 如无路径,文件在程序文件同一目录下寻找
      string so("x:\testout.txt");
      ifstream inf(si.c_str()); // 以C语言形式的字符串,或可用字面字符串,但不可用Cpp的sting
      ofstream outf(so.c_str());
      
      /*
      默认时,与 ifstream 流对象关联的文件将以 in 模式打开,该模式允许文件做读的操作。
      与 ofstream 关联的文件则以 out 模式打开,使文件可写。
      所以不用设置文件模式,取默认的即可。
      */
      
      if(!inf){ //读入失败,中断操作
        cerr << "error: unable to open input file: "
             << si << endl;
        return -1;
      }
    
      if(!outf){ // 这句进不了,因为没有输出文件的话,会新建一个
        cerr << "error: unable to open output file: "
             << so << endl;
        return -1;
      }  
      
      int i,j;
      inf>>i>>j;        // 从文件输入流中读取
      outf<<i+j<<endl;  // 输出到文件输出流
      
      cout << "en,it's done!" << endl; // 中间不影响键盘输入、屏蔽输出
      
      inf.close();
      outf.close();
      
      return 0;
    }
    
    
    // 重定向。但因为cin慢,所以比赛时(OJ时)还得用C语言的版本,scanf..., freopen...
    
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    { 
      ifstream cin("x:\testin.txt"); // 只要cin重定向,就会从文件中读取
      ofstream cout("x:\testout.txt"); // 复赛时,用这种方法重定向(当然不能写路径)也比较方便
      
      int i,j;
      cin>>i>>j;
      cout<<i+j<<endl;
     
      cin.close();
      cout.close();
      
      return 0;
    }
    
    
    
    //8.5 字符串流 ---------------------------------------------------------------------------------
    
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    { 
      stringstream strm("test");
      cout << strm << endl; // 貌似是个地址
      cout << strm.str() << endl;
      string s("test too");
      strm.str(s);
      cout << strm.str() << endl;
      strm.str(""); // 如无这句,观察输出?strm.clear()木用
      strm << "aha";
      cout << strm.str() << endl;
    
      return 0;
    }
    
    
    // 模拟输入流
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      string line, word; // will hold a line and word from input, respectively
      while(getline(cin, line))
      {
        // read a line from the input into line
        // do per-line processing
        istringstream stream(line); // bind to stream to the line we read
        while(stream >> word)       // 可以理解为模拟输入流
        {
          // read a word from line
          // do per-word processing
          cout << word << endl;
        }
      }
      return 0;
    }
    
    
    // 既然可以模拟输入流,也可以读取整数了
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      string line;
      int word,sum(0);
      while(getline(cin, line))
      {
        istringstream stream(line);
        while(stream >> word) // 模拟读取整数
        {
          sum+=word;
          cout << word << endl;
        }
      }
      cout << sum << endl;
      
      return 0;
    }
    
    
    // 同样,可以读取实数
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      string line;
      double word,sum(0);
      while(cin>>line) { // not line anymore
        istringstream stream(line);
        stream >> word;  // not word anymore
        sum+=word;
      }
      cout << sum << endl;
      
      return 0;
    }
    
    
    // 字符串,转换成数的程序:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      string s("3.14");
      double d;
      istringstream stream(s);
      stream >> d;
      cout << d << endl;
      
      return 0;
    }
    
    
    // 模拟输出
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      int val1 = 512, val2 = 1024;
      ostringstream format_message;
      // ok: converts values to a string representation
      format_message << "val1: " << val1 << "
    " 
        << "val2: " << val2 << "
    ";
      cout << format_message.str();
      return 0;
    }
    
    
    
    // mytest
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      string s("3.14");
      ostringstream os;
      os << "test" << s;
      cout << os.str() << endl;
      
      return 0;
    }
    
    
    // 这样,就将数轮换成了字符串
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      double d(3.14);
      ostringstream os;
      os << d;
      string s=os.str();
      cout << s << endl;
    
      return 0;
    }
    
    
    // 终于可以将 bitset 数串,保存为字符串了
    
    #include <iostream>
    #include <sstream>
    #include <bitset>
    #include <string>
    
    using namespace std;
    int main()
    {
      bitset<32> b(2147483647);
      cout << b << endl;
      ostringstream os;
      os << b;
      string s=os.str();
      cout << s << endl;
    
      return 0;
    }
    
    
    // ***stringstream 对象的一个常见用法是,需要在多种数据类型之间实现自动格式化时使用该类类型。***
    
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
      int val1 = 512, val2 = 1024;
      ostringstream format_message;
      // ok: converts values to a string representation
      format_message << "val1: " << val1 << "
    " 
        << "val2: " << val2 << "
    ";
      cout << format_message.str(); // 整数转换成一串字符串
      // str member obtains the string associated with a stringstream
      istringstream input_istring(format_message.str());
      string dump; // place to dump the labels from the formatted message
      // extracts the stored ascii values, converting back to arithmetic types
      input_istring >> dump >> val1 >> dump >> val2; // 从字符串中,把数字恢复过来。模拟屏幕输入
      cout << val1 << " " << val2 << endl;  // prints 512 1024
    
      return 0;
    }
    
    
    // 关于数、串互转的贴子:http://blog.sina.com.cn/s/blog_78fd98af0100v3ib.html

    TOP

  • 相关阅读:
    Linux文件系统的设计
    HTML中Select的使用具体解释
    【大话设计模式】—— 工厂方法模式
    C++ Primer 学习笔记_84_模板与泛型编程 --模板特化
    Arcgis API for Android之GPS定位
    “大型票务系统”中对机器恶意訪问的处理——验证码
    hdu 4611
    Java实现 蓝桥杯VIP 算法训练 ALGO-85进制转换
    Java实现 蓝桥杯VIP 算法训练 摆动序列
    Java实现 蓝桥杯VIP 算法训练 摆动序列
  • 原文地址:https://www.cnblogs.com/xin-le/p/4088167.html
Copyright © 2020-2023  润新知