freopen()函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了freopen()函数相关的知识,希望对你有一定的参考价值。

在网上看到这样一段代码。不太懂,如下:
#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( "file.txt", "w", stdout ); //把内容写到这个文件"file.txt"
此语句使所有的后续输出,通常定向到转到文件 FILE.TXT stdout,向。

若要返回到显示默认 stdout) 的 stdout,使用下面的调用:
freopen( "CON", "w", stdout ); //输出到控制台"CON"
在这两种情况下检查 freopen() 以确保重定向实际发生的返回值。

下面是短程序演示了 stdout 时重定向:
运行代码

// Compile options needed: none
#include <stdio.h>
#include <stdlib.h>void main(void)

FILE *stream ;

//将内容写到file.txt, "W"是写 ("r"是读)
if((stream = freopen("file.txt", "w", stdout)) == NULL)
exit(-1);
printf("this is stdout output\n");
stream = freopen("CON", "w", stdout);stdout 是向程序的末尾的控制台重定向
printf("And now back to the console once again\n");


"CON" 是指控制台 就想DOS窗口.
==========================================
运行代码:
#include <stdio.h>
#include <stdlib.h>

void main(void)

FILE *stream ;
char s[102400]="";
if((stream = freopen("file.txt", "r", stdin)) == NULL) //从文件读数据 (放到stdin , 其实stdin 也有自己的缓冲区.就向buf)
exit(-1);

fread(s, 1, 1024, stdin); //所以从标准输入里读出数据. 因为要注意stdin也是有自己的一块缓冲区.

printf("%s\n", s); //在这里打印读出来的数据.

参考技术A stdout是标准输出文件,就是显示器,这是Unix里的用法。 参考技术B stdout<stdio.h>中定义的全局变量(FILE),标准输出,就是显示器。对应的标准输入是stdin,就是键盘。可以用freopen把它们重定向到其他文件。ctrl+z在普通PC上是文件结束符。 参考技术C standard output,控制台的标准输出端

freopen函数的用法

如题

freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流。该函数可以在不改变代码原貌的情况下改变输入输出环境,但使用时应当保证流是可靠的.
头文件:stdio.h
C89函数声明:
FILE *freopen( const char *filename, const char *mode, FILE *stream );[1]
C99函数声明:
FILE *freopen(const char * restrict filename, const char * restrict mode, FILE * restrict stream);
形参说明:
filename:需要重定向到的文件名或文件路径。
mode:代表文件访问权限的字符串。例如,"r"表示“只读访问”、"w"表示“只写访问”、"a"表示“追加写入”。
stream:需要被重定向的文件流。
返回值:如果成功,则返回该指向该输出流的文件指针,否则返回为NULL。
参考技术A 输入输出重定向
例:文件名为:r.txt
输入重定向到文件r.txt:freopen("r.txt","r",stdin);
输出到文件:freopen("r.txt","w",stdout);
参数说明:(文件名,方式(r为读取,w为写入),照打就ok了);
若文件名出写:"CON",为重定向回控制台

但这个函数好像和system的函数有点不合,具体在用完重定向输出回控制台后,再用system的函数后再试试输出 你就懂了....本回答被提问者采纳

以上是关于freopen()函数的主要内容,如果未能解决你的问题,请参考以下文章

freopen函数的用法

freopen函数怎么使用

freopen函数的使用以及freopen与fopen的区别

C语言中,freopen,studin,studout有啥用

C++ 中 freopen()函数的用法

freopen重定向输入