IO Redirection in C++ (redirect cin/cout to file stream, and restore)
来源:互联网 发布:gabrielle名字 知乎 编辑:程序博客网 时间:2024/06/06 00:29
Author: YuMaNzI 2014/01/19
#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 << "\n"; //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}
0 0
- IO Redirection in C++ (redirect cin/cout to file stream, and restore)
- Redirect the stream buffer of cout to a file with ios::rdbuf()
- How to deal with Whitespaces in scanf, gets(in C) and cin, getline(in C++) ?
- Using the LDF File and Old Backup File to Restore the Data Base in SQL SERVER 2005
- 【C/C++】Cin与Cout
- <C++>_浅谈cin&cout
- 标准IO流cin与cout
- Bash Commands - the diff of > and >> # Use I/O Redirection to generate a log file
- STM32F4 Discovery and printf() redirection to debug viewer in Keil MDK-ARM
- out和cout in和cin
- redirect the std::cout to QtTextEdit
- How to backup and restore database in SQL Server
- [c++]C++学习笔记001.cout/cin
- cin/cout
- cin & cout
- cin & cout
- cin &&cout
- cin cout
- Compile FFmpeg on Ubuntu
- Hessian接口学习笔记
- Android打电话程序(根据添加按钮监听的3种不同方式分别实现)
- koala jbpm
- Burp Suite应用分享之Web漏洞扫描
- IO Redirection in C++ (redirect cin/cout to file stream, and restore)
- thinkphp3.2开发排错一例——D方法与M方法的使用
- 深入研究 Java Synchronize 和 Lock 的区别与用法
- Tomcat6下应用太多出现java.lang.OutOfMemoryError: PermGen space【解决办法】
- php查漏补缺之变量(一)
- Linux CPU Utilization
- 互联网系统架构的演进
- android4.0新控件Switch方法解析
- Linux 设备驱动 如何自动创建设备文件