久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將cin和cout重定向到文件?

How to redirect cin and cout to files?(如何將cin和cout重定向到文件?)
本文介紹了如何將cin和cout重定向到文件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何將 cin 重定向到 in.txt 并將 cout 重定向到 out.txt?

How can I redirect cin to in.txt and cout to out.txt?

推薦答案

這是您想要執行的操作的示例.閱讀注釋以了解代碼中每一行的作用.我已經在我的電腦上用 gcc 4.6.1 測試過它;它工作正常.

Here is an working example of what you want to do. Read the comments to know what each line in the code does. I've tested it on my pc with gcc 4.6.1; it works fine.

#include <iostream>
#include <fstream>
#include <string>

void f()
{
    std::string line;
    while(std::getline(std::cin, line))  //input from the file in.txt
    {
        std::cout << line << "
";   //output to the file out.txt
    }
}
int main()
{
    std::ifstream in("in.txt");
    std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
    std::cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!

    std::ofstream out("out.txt");
    std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!

    std::string word;
    std::cin >> word;           //input from the file in.txt
    std::cout << word << "  ";  //output to the file out.txt

    f(); //call function


    std::cin.rdbuf(cinbuf);   //reset to standard input again
    std::cout.rdbuf(coutbuf); //reset to standard output again

    std::cin >> word;   //input from the standard input
    std::cout << word;  //output to the standard input
}

您可以在一行中保存重定向:

auto cinbuf = std::cin.rdbuf(in.rdbuf()); //save and redirect

此處 std::cin.rdbuf(in.rdbuf())std::cin's 緩沖區設置為 in.rdbuf()然后返回與 std::cin 關聯的舊緩沖區.使用 std::cout 也可以做到同樣的事情 —或任何.

Here std::cin.rdbuf(in.rdbuf()) sets std::cin's buffer to in.rdbuf() and then returns the old buffer associated with std::cin. The very same can be done with std::cout — or any stream for that matter.

希望有所幫助.

這篇關于如何將cin和cout重定向到文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 中文字幕成人在线 | 欧美日韩一区二区三区四区 | 二区三区av| 久久国产成人午夜av影院武则天 | 粉嫩一区二区三区国产精品 | www.玖玖玖| 成人av播放 | av手机在线播放 | 免费在线日韩 | 久久国产精品免费一区二区三区 | 特黄视频 | 精品久久久久久亚洲综合网站 | 国产精品久久久久久久久久久免费看 | 欧美成人高清 | 成人免费观看男女羞羞视频 | 亚洲午夜精品视频 | av在线播放网址 | 干干干操操操 | 隔壁老王国产在线精品 | aaaaaaa片毛片免费观看 | 国产精品久久久久久二区 | 成人久久久| 人人干人人爽 | 在线视频一区二区三区 | 国产精品美女久久久 | 国产夜恋视频在线观看 | 日韩在线国产 | 国产一区二区影院 | 免费观看成人鲁鲁鲁鲁鲁视频 | 精品亚洲国产成av人片传媒 | 成年人精品视频在线观看 | 国色天香综合网 | 成人视屏在线观看 | .国产精品成人自产拍在线观看6 | 国产一区 在线视频 | 国产精品久久在线观看 | 黄色网址大全在线观看 | www.蜜桃av | 亚洲精品视频一区二区三区 | 国产乱码精品一区二区三区中文 | 91精品国产日韩91久久久久久 |