zoukankan      html  css  js  c++  java
  • 其他:OI竞赛中的文件操作

    本文介绍三种方法进行文件输入输出,都非常实用

    第一种方法是采用重定向的形式进行输入输出,很方便

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    记得包含头文件#include<cstdio>

    第二种方法是使用C语言的形式进行输入输出

     1 #include<cstdio>
     2 #define INF 1000000000
     3 using namespace std;
     4 int main()
     5 {
     6     FILE *fin,*fout;
     7     fin=fopen("input.txt","rb");
     8     fout=fopen("output.txt","wb");
     9     int a,b;
    10     fscanf(fin,"%d%d",&a,&b);
    11     fprintf(fout,"%d
    ",a+b);
    12     return 0;
    13 }

    第三种方法是C++推荐的形式,以流的形式输入输出

     1 #include<fstream>
     2 using namespace std;
     3 ifstream in("input.txt");
     4 ofstream out("output.txt");
     5 int main()
     6 {
     7     int a,b; 
     8     in>>a>>b;
     9     out<<a+b;
    10     return 0;
    11 }

    OI选手推荐使用第一种方式

  • 相关阅读:
    对我影响最大的三位老师
    自我介绍
    第二周作业
    2019第一次作业
    PTA编程总结3
    币值转换
    PTA编程总结2
    PTA编程总结1
    秋季学期学习总结
    人生路上对你影响最大的三位老师
  • 原文地址:https://www.cnblogs.com/aininot260/p/9271353.html
Copyright © 2011-2022 走看看