freopen()函数

来源:百度知道 编辑:UC知道 时间:2024/05/29 22:10:37
在网上看到这样一段代码。不太懂,如下:
#include <stdio.h>
char s[1000];

void main() {
freopen("c:\\b.txt","w",stdout);
while (gets(s)) printf("%s\n",s);
}
上述的代码中的freopen("b.txt","w",stdout);这句。stdout表示什么?
为什么输入的字符存入文件b.txt中?为什么用ctrl+z可以结束程序。

函数名: freopen
功 能: 替换一个流
用 法: FILE *freopen(char *filename, char *type, FILE *stream);
程序例:
#include <stdio.h>
int main(void)
{
/* redirect standard output to a file */
if (freopen("OUTPUT.FIL", "w", stdout)
== NULL)
fprintf(stderr, "error redirectingstdout\n");
/* this output will go to a file */
printf("This will go into a file.");
/* close the standard output stream */
fclose(stdout);
return 0;
}

===========================================================
上面不懂, 可以向下看, 没关系. 实践+理论 , 会慢慢在这详解.., 慢慢看.
下面重点:

在这再说一下. 不然很难理解, 我都没想到. 一直困惑不清啊....stdin stdout stderr.
现在懂了.

牢记: 目前主要的缓存特征是:stdin和stdout是行缓存;而stderr是无缓存的。
本文介绍如何将 stdout 时重定向到文件从某个 C 的程序,然后还原原始的 stdout 同一程序的某个更高位置。 C 函数通常用于重定向 stdout 或 stdin 是 freopen()。 要将 stdout 时重定向到文件名为 FILE.TXT 中,使用下面的调用:
freopen( "fi