/*
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
}
*/

  

C/C++-标准输入/输出重定向为文件输入/输出的更多相关文章

  1. [转]C语言文件输入/输出ACM改进版(freopen函数)

    C语言文件输入/输出ACM改进版(freopen函数) 2009年5月27日 10:379,457 浏览数发表评论阅读评论   文章作者:姜南(Slyar) 文章来源:Slyar Home (www. ...

  2. 将make的输出重定向到文件(转)

    原文:将make的输出重定向到文件 [用法] 1.想要把make输出的全部信息,输出到某个文件中,最常见的办法就是:make xxx > build_output.txt此时默认情况是没有改变2 ...

  3. Perl读取标准输入<STDIN>、读取文件输入<>和chomp函数

    读取标准输入<STDIN> <STDIN>表示从标准输入中读取内容,如果没有,则等待输入.<STDIN>读取到的结果中,如果没有意外,都会自带换行符. 例如,tes ...

  4. Make 输出重定向到文件

    系统的输入与输出: 方式 描述符 含义 stdin 0 标准输入 stdout 1 标准输出 stderr 2 标准错误输出 把 make 输出的全部信息重定向到某个文件中: make <xxx ...

  5. 【C语言学习】《C Primer Plus》第13章 文件输入/输出

    学习总结 1.文件函数原型1: FILE* fopen(char *filename, char *openmode);  //打开文件,返回文件指针 filename:文件名,更确切地说,是包含文件 ...

  6. C Primer Plus之文件输入/输出

    文件 一个文件通常就是磁盘上的一段命名的存储区.但对于操作系统来说,文件就会更复杂一些.例如,一个大文件可以存储在一些分散的区段中,或者还会包含一些使操作系统可以确定其文件类型的附加数据. C将文件看 ...

  7. Linux实时将所有输出重定向到文件

    Linux的重定向机制十分好用,我们经常需要在服务器上挂起一个服务程序,然后将该程序的所有输出重定向到某个文件,这样即使我们注销了用户,程序依然在linux服务器上运行着. 但是重定向的输出经常无法实 ...

  8. 转:Linux实时将所有输出重定向到文件

    转自: Linux的重定向机制十分好用,我们经常需要在服务器上挂起一个服务程序,然后将该程序的所有输出重定向到某个文件,这样即使我们注销了用户,程序依然在linux服务器上运行着. 但是重定向的输出经 ...

  9. 如何将top命令输出重定向为文件

    命令: # top -b -n 2 -d 3 > /tmp/top.out 解析: -b: batch 模式,可以重定向到文件中 -n:一共取2次top数据 -d:每次top数据间隔为3秒

随机推荐

  1. js页面加载完后执行(document.onreadystatechange 和 document.readyState)

    js页面加载完后执行javascript(document.onreadystatechange 和 document.readyState) document.onreadystatechange ...

  2. Bash - 趣味Shell

    好消息:本人博客已开通.很多其它博客请进 http://swplzj.github.io 今天抽闲玩了一下Shell.发现几个比較有意思的.记录一下. 终端彩虹字:lolcat 图形化文字:figle ...

  3. Android:相机适配及图片处理的一些问题

    链接:http://www.cnblogs.com/liushilin/p/6387263.html 链接:http://www.cnblogs.com/liushilin/p/5956691.htm ...

  4. 菜鸟学SSH(十七)——基于注解的SSH将配置精简到极致

    很早之前就想写一篇关于SSH整合的博客了,但是一直觉得使用SSH的时候那么多的配置文件,严重破坏了我们代码整体性,比如你要看两个实体的关系还得对照*.hbm.xml文件,要屡清一个Action可能需要 ...

  5. FFmpeg(13)-创建OpenSLES混音器CreateOutputMix初始化

    一.包含头文件和库文件 CMakeLists target_link_libraries( # Specifies the target library. native-lib OpenSLES # ...

  6. CCDictionary

    /** * CCDictionary is a class like NSDictionary in Obj-C . * * @note Only the pointer of CCObject or ...

  7. 每日英语:Foreign Tourists Skip Beijing

    Overseas tourists continued to shun Beijing through 2013. shun:避开,避免,回避 Amid rising pollution and a ...

  8. Ubuntu首次安装后root权限解锁

    在ubuntu系统下,为了安全起见,在安装过程中,系统屏蔽了用户设置root用户.导致很多用户在使用过程中不知道root密码到底是什么. 可以使用如下方法解决: 先解除root锁定,为root用户设置 ...

  9. vim 配色(mac)

    1.进入当前用户目录,新建 .vimrc ,并加入如下内容: cd / vim .vimrc 2.开启行号 选择颜色 #开启行号 set nu #选择颜色 colorscheme desert syn ...

  10. java多线程14 :wait()和notify()/notifyAll()

    轮询 线程本身是操作系统中独立的个体,但是线程与线程之间不是独立的个体,因为它们彼此之间要相互通信和协作. 想像一个场景,A线程做int型变量i的累加操作,B线程等待i到了10000就打印出i,怎么处 ...