问题: 我如何才能把标准输入输出窗口暂时重定向到一个文件?
解答: 为了暂时重定向标准的输出输出(如
例1:
这个例子把行信息打印到stdio窗口,然后再有一行到文件,接着又有一行到stdio屏幕。
例2:
这个例子从屏幕读取一个数,然后从文件中读取第一个单词(从第一个空格算起),然后又从控制台读取一个字符串。
相关链接: LabWindows/CVI Support
附件:
解答: 为了暂时重定向标准的输出输出(如
printf
和
scanf
函数实现输入输出)到一个文件,你需要改变
stdin
和
stdout
文件指针。下面两个例子显示具体实现方法:
例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
附件: