全部博文(118)
分类: LINUX
2008-10-22 16:49:55
printf
和 scanf
函数实现输入输出)到一个文件,你需要改变stdin
和 stdout
文件指针。下面两个例子显示具体实现方法:#include
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;
}
#include
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;
}