首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

freopen() 输入输出重定向(到资料)、打表格

2012-08-22 
freopen() 输入输出重定向(到文件)、打表格一、freopen()输出输出重定向(到文件)?????? 当我们求解acm题目时

freopen() 输入输出重定向(到文件)、打表格

一、freopen()输出输出重定向(到文件)

?

????? 当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC等)中运行程序,输入测试数据,当能得到正确运行结果后,才将程序提交到oj中。但由于调试往往不能一次成功,每次运行时,都要重新输入一遍测试数据,对于有大量输入数据的题目,输入数据需要花费大量时间。
????? 使用freopen函数可以解决测试数据输入问题,避免重复输入,不失为一种简单而有效的解决方法。

函数名:freopen
声明:FILE *freopen( const char *path, const char *mode, FILE *stream );
所在文件: stdio.h
参数说明:
path: 文件名,用于存储输入输出的自定义文件名。
mode: 文件打开的模式。和fopen中的模式(如r-只读, w-写)相同。
stream: 一个文件,通常使用标准流文件。
返回值:成功,则返回一个path所指定文件的指针;失败,返回NULL。(一般可以不使用它的返回值)
功能:实现重定向,把预定义的标准流文件定向到由path指定的文件中。标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;stderr是标准错误流,一般把屏幕设为默认。


??????? 下面以在VC下调试“计算a+b”的程序举例。

文件位置示意:

A. 在VS工程中,文件路径是相对于Test.cpp而言的

//VS工程中的结构:--我的工程     --Debug          --Test.exe     --我的工程          --Test.cpp          --Debug               --in.txt               --out.txt

B. 在实际应用中(例如,将Test.exe复制出来作为一个单独的程序使用),文件路径则是相对于Test.exe而言的

--Test.exe--Debug     --in.txt     --out.txt          
?


C语法:

#include<stdio.h>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<iostream>using namespace std;int main(){int a,b;freopen("Debug\\in.txt","r",stdin);freopen("Debug\\out.txt","w",stdout);while(cin>>a>>b){//和C唯一的区别就是输入使用>>,而非scanf();输出使用<<,而非printf()cout<<a+b<<endl;//和C唯一的区别就是输入使用>>,而非scanf();输出使用<<,而非printf()}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时不要忘记把与重定向有关的语句删除:)

?

二、打表格csv

看一个程序就明白鸟:)

/*线段树空间分析程序 Power By:Comzyh*/  #include <iostream>  #include <cstdio>  #include <cstdlib>  using namespace std;  struct segment {         int b,e;         };  segment seg[5000000];  int N;  int Nnode,LastNode;  void build(int b, int e, int s);  int main(){      freopen ("segmentCount.csv","w",stdout);      int i=1;      scanf("%d",&N);      printf("区间长度,节点数,最后一个节点编号\n");      while (N-i>=0){            Nnode=0;            LastNode=0;            build(1,i,1);            printf("%d,%d,%d\n",i,Nnode,LastNode);                        i++;            }      //system("pause");      }  void build(int b, int e, int s){       Nnode++;       if (s>LastNode)          LastNode=s;       seg[s].b=b;       seg[s].e=e;       if (b==e)           return;       int mid =(b+e)>>1;       build(b,mid,s<<1);       build(mid+1,e,(s<<1)+1);       } 
?

?

热点排行