重定向到文件(+vc小提示)



暂时发现的重定向的意义:将测试数据储存在某一特定文件中,每次运行程序时自动提取,

能节省大量从控制台(console)输入数据的时间,尤其是在搞定ACM题的时候。

#pragma warning(disable:4996)                                  //此句直接跳过error C4996,可以使程序正常运行并成功重定向。     

                                                                                     //错误 1 error C4996: 'freopen': This function or variable may be unsafe. 

                                                                                     //Consider using freopen_s instead.

#include<stdio.h>
#define LOCAL                                                            //只有定义了LOCAL才会编译两条freopen语句。
#define INF 1000000000
int main(void)
{
#ifdef LOCAL
 freopen("data.txt", "r", stdin);                                      //data.txt中含有测试数据,文件后缀为.txt好处是方便修改测试数据;

                                                                                    //另外,data.txt文件应该与相应C的源代码文件放在同一文件夹下。
 freopen("dataout.txt", "w", stdout);                             //dataout.txt中含有程序运行结果,方便查看。
#endif
 int x, n = 0, min = INF, max = -INF, s = 0;
 while (scanf_s("%d", &x) == 1)
 {
  s += x;
  if (x < min) min = x;
  if (x>max) max = x;
 }
 n++;
 printf("%d %d %.3f\n", min, max, (double)s / n);
 getchar();
 getchar();
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章