From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.



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



主要软件: LabWindows/CVI Development Systems>>LabWindows/CVI Full Development System
主要软件版本: 4.0.1
主要软件修正版本: N/A
次要软件: N/A

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

解答: 为了暂时重定向标准的输出输出(如 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

附件:





报告日期: 11/15/2006
最近更新: 11/22/2006
文档编号: 0X6EEB2Q