标准输入输出重定向

方法1:

//使用freopen_s()函数
#include <stdio.h>
int main() 
{
//将控制台的标准输入输出改成从文件中读取写入
FILE *pFileRead ;
FILE *pFileWrite;
FILE *pStdint;
pStdint = stdin;
FILE *pStdout;
pStdout = stdout;
errno_t err;
printf( "将控制台的标准输入输出改成从文件中读取写入\n");
err = freopen_s(&pFileRead, "infile.txt", "r", stdin);
if (err != 0)
{
printf( "pFileRead错误freopen\n");
return 0;
}
err = freopen_s(&pFileWrite, "outfile.txt", "w", stdout);
if (err != 0)
{
printf( "pFileWrite错误freopen\n");
return 0;
}
int n;
while (scanf_s("%d", &n) != EOF)
{
n *= 2;
printf("%d\n", n);
}
printf("结束01 结束输出\n");
//回到到控制台的标准输入输出 windows为"CON" linux为"/dev/console"
err = freopen_s(&pStdint, "CON", "r", pFileRead);
if (err != 0)
{
printf("pStdout错误freopen\n");
fclose(pFileRead);
fclose(pFileWrite);
return 0;
}
err = freopen_s(&pStdout, "CON", "w", pFileWrite);
if (err != 0)
{
printf( "pStdout错误freopen\n");
fclose(pFileRead);
fclose(pFileWrite);
return 0;
}
printf( "结束02 输入0表示结束---\n");
do{
scanf_s("%d", &n);
} while (n != 0);
fclose(pFileRead);
fclose(pFileWrite);
return 0;
}



//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------


方法2:

//使用ifstream和ofstream及cin和cout的rdbuf()
#include"stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
printf("   使用ifstream和ofstream及cin和cout的rdbuf()来改变控制台的标准输入输出\n");
printf("  --by MoreWindows( http://blog.csdn.net/MoreWindows )--\n\n");
//将控制台的标准输入输出改成从文件中读取写入
ifstream inFile("infile.txt");
ofstream outFile("outfile.txt");
//保存原来的输入输出方式 streambuf类就是basic_streambuf类
streambuf *strmin_buf = cin.rdbuf();
streambuf *strmout_buf = cout.rdbuf();


printf("开始处理文件\n....\n");
//重定向到文件
cin.rdbuf(inFile.rdbuf());
cout.rdbuf(outFile.rdbuf());


//原程序代码
int n;
while (cin >> n)
{
n *= 2;
cout << n << endl;
}


inFile.close();
outFile.close();


//回到控制台的标准输入输出
cin.rdbuf(strmin_buf);
cout.rdbuf(strmout_buf);


cout << "文件已经处理完毕 输入0表示结束:" << endl;
do{
cin >> n;
} while (n != 0);


return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言标准库中有一个函数叫做freopen(),它可以用于重定向C程序的输入和输出流。通过调用freopen()函数,我们可以将标准输入流stdin标准输出流stdout重定向到文件或者管道中。 要将C程序的标准输入流重定向到管道,可以使用如下命令: ``` freopen("input.txt", "r", stdin); ``` 这会将程序的标准输入流重定向到名为"input.txt"的文件中。这样,程序执行时会将文件中的内容作为输入。 同样地,要将C程序的标准输出流重定向到管道,可以使用如下命令: ``` freopen("output.txt", "w", stdout); ``` 这会将程序的标准输出流重定向到名为"output.txt"的文件中。这样,程序执行后的输出会被写入到文件中。 需要注意的是,freopen()函数也可以对C中的cin和cout进行重定向。使用方法与上述类似,只需要将文件名替换为管道的名称即可。 通过这种方式,我们可以在C程序中实现输入输出重定向,从而实现输入和输出的管道操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++输入输出重定向(3种方法)](https://blog.csdn.net/weixin_39536630/article/details/116964030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值