freopen头文件

freopen头文件freopen C C 文件输入输出利器 freopen 以前经常使用 比较方便 可以当作模板 在中间替换为自己的代码即可使用 include 实际使用中发现 freopen 也包含在 iostream h 中 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp

freopen – C/C++文件输入输出利器

freopen以前经常使用,比较方便,可以当作模板,在中间替换为自己的代码即可使用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include

//
实际使用中发现freopen也包含在iostream.h中,

                              //C++代码#include

即可。

              

int main()

{

     freopen(“sample.in”, “r”, stdin);

     freopen(“sample.out”, “w”, stdout);

              

     /* 同控制台输入输出 */

              

     fclose(stdin);

     fclose(stdout);

 

     return 0;

}

—–转自:http://www.slyar.com/blog/c-freopen-stdin-stdout.html  ————-

当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC 等)中运行程序,输入测试数据,当能得到正确运行结果后,才将程序提交到oj中。但由于调试往往不能一次成功,每次运行时,都要重新输入一遍测试数据,对于有大量输入数据的题目,输入数据需要花费大量时间。 

        使用freopen函数可以解决测试数据输入问题,避免重复输入,不失为一种简单而有效的解决方法。 

 

函数名:freopen 

声明:FILE *freopen( const char *path, constchar *mode, FILE *stream ); 

所在文件: stdio.h 

参数说明: 
path:
文件名,用于存储输入输出的自定义文件名。 
mode:
文件打开的模式。和fopen中的模式(如r-只读, w-写)相同。 
stream:
一个文件,通常使用标准流文件。 

返回值:成功,则返回一个path所指定文件的指针;失败,返回NULL。(一般可以不使用它的返回值) 

功能:实现重定向,把预定义的标准流文件定向到由path指定的文件中。标准流文件具体是指stdinstdoutstderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;stderr是标准错误流,一般把屏幕设为默认。 

       


下面以在VC下调试计算a+b”的程序举例。 
C
语法: 
#include

 

int main() 

int a,b; 
freopen(“debug\\in.txt”,”r”,stdin);





//输入重定向,输入数据将从in.txt文件中读取 

freopen(“debug\\out.txt”,”w”,stdout);

//输出重定向,输出数据将保存在out.txt文件中 
while(scanf(“%d %d”,&a,&b)!=EOF) 
printf(“%d\n”,a+b); 
fclose(stdin);//


关闭文件 
fclose(stdout);//
关闭文件 
return 0; 

C++



语法 
#include

 

#include

 

int main() 

int a,b; 
freopen(“debug\\in.txt”,”r”,stdin); //







输入重定向,输入数据将从in.txt文件中读取 
freopen(“debug\\out.txt”,”w”,stdout); //
输出重定向,输出数据将保存在out.txt文件
while(cin>>a>>b) 
cout<
注意使用
endl 
fclose(stdin);//

关闭文件
 
fclose(stdout);//

关闭文件
 
return 0; 

       freopen(“debug\\in.txt”,”r”,stdin)



的作用就是把标准输入流
stdin
重定向到
debug\\in.txt
文件中,这
样在用
scanf
或是用
cin
输入时便不会从标准输入流读取数据
,
而是从
in.txt
文件中获取输入。只要把输入数据事先粘贴到
in.txt
,调试时就方
便多了。
 


类似的,
freopen(“debug\\out.txt”,”w”,stdout)
的作用就是把
stdout
重定向到
debug\\out.txt
文件中,这样输出结果需要打开
out.txt
文件查看。
 

       


需要说明的是:
 
        1.


freopen(“debug\\in.txt”,”r”,stdin)
中,将输入文件
in.txt
放在文件夹
debug
中,文件夹
debug
是在
VC
中建立工程文件时自动生成的调试文件夹。如果改成
freopen(“in.txt”,”r”,stdin)
,则
in.txt
文件将放在所建立的工程文件夹
下。
in.txt
文件也可以放在其他的文件夹下,所在路径写正确即可。
 
        2.

可以不使用输出重定向,仍然在控制台查看输出。
 
        3.

程序调试成功后,提交到
oj
时不要忘记把与重定向有关的语句删除。


 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/225534.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 上午9:06
下一篇 2026年3月17日 上午9:06


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号