zoukankan      html  css  js  c++  java
  • C/C++-标准输入/输出重定向为文件输入/输出

    /*
        Time: 2017-02-22 11:11:15
        Describe: C++程序将标准输入/输出重定向为文件输入/输出。
    */
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
     
    void f()
    {
        string line;
        while(getline(cin, line))  //input from the file in.txt
        {
            cout << line <<endl;   //output to the file out.txt
        }
    }
    
    // TEST ONE.
    int main()
    {
        ifstream in("in.txt");
        streambuf *backinbuf = cin.rdbuf(); //save old buf
        cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
     
        ofstream out("out.txt");
        streambuf *backoutbuf = cout.rdbuf(); //save old buf
        cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
     
        // ONE.
        f(); //call function
    }
    
    // TEST TWO.
    /*
    int main()
    {
        ifstream in("in.txt");
        streambuf *backinbuf = cin.rdbuf(); //save old buf
        cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
     
        ofstream out("out.txt");
        streambuf *backoutbuf = cout.rdbuf(); //save old buf
        cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    
        // TWO.
        string word;
        cin >> word;           //input from the file in.txt
        cout << word << "--";  //output to the file out.txt
    }
    */
    
    // TEST THREE.
    /*
    int main()
    {
        ifstream in("in.txt");
        streambuf * backinbuf = cin.rdbuf(); //save old buf
        cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
     
        ofstream out("out.txt");
        streambuf * backoutbuf = cout.rdbuf(); //save old buf
        cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    
        // DO SOMETHING...... (redirect.)
    
        // RESET.
        cin.rdbuf(backinbuf);   //reset to standard input again
        cout.rdbuf(backoutbuf); //reset to standard output again
     
        // THREE.
        cin >> word;   //input from the standard input
        cout << word;  //output to the standard input
    }
    */
    

      

  • 相关阅读:
    014-CallbackServlet代码
    PaymentServlet代码
    013-PaymentUtils工具类模板
    案例30-在线支付
    案例29-购物车提交订单
    案例28-清空购物车
    案例27-购物车删除单一商品
    案例26-购物车
    案例25-servlet的抽取
    在Eclipse或工作空间中 ,复制或修改项目后,把项目部署后发现还是原来的项目名称
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/6427975.html
Copyright © 2011-2022 走看看