对一个标准的输入/输出窗口临时重定向到一个文件

问题: 我如何才能把标准输入输出窗口暂时重定向到一个文件?

解答: 为了暂时重定向标准的输出输出(如 printfscanf 函数实现输入输出)到一个文件,你需要改变 stdinstdout 文件指针。下面两个例子显示具体实现方法:

例1:

这个例子把行信息打印到stdio窗口,然后再有一行到文件,接着又有一行到stdio屏幕。

#include <ansi_c.h>
int main (int argc, char *argv[])
{
FILE *copy;

printf ("This is printed to screen!/n");
copy = stdout;
stdout = fopen ("stdio test.txt", "w");
printf ("This is printed to a file!/n");
fclose (stdout);
stdout = copy;
printf ("Where is this line?/n");
return 0;
}



例2:

这个例子从屏幕读取一个数,然后从文件中读取第一个单词(从第一个空格算起),然后又从控制台读取一个字符串。

#include <ansi_c.h>
int main (int argc, char *argv[])
{
FILE *copy;
int x;
char y[100] = "/0";

printf ("This is printed to screen!/n");
scanf ("%d", &x);
copy = stdin;
stdin = fopen ("stdio test.txt", "r");
scanf ("%s", y);
fclose (stdin);
stdin = copy;
printf ("%s/n", y);
scanf ("%s", y);
printf ("%s/n", y);
return 0;
}


相关链接: LabWindows/CVI Support

附件: 

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值