zoukankan      html  css  js  c++  java
  • [C++]竞赛模板·数据统计与IO(重定向版与非重定向版)

     
    /*
        数据统计与IO 重定向版模板
    
        描述:本机测试用文件数据流重定向,一旦提交到比赛就自动“删除”重定向语句
    */
    # define LOCAL
    #include<stdio.h>
    #include<time.h>
    
    int main(){
    #ifdef LOCAL
        freopen("data.in","r", stdin);
        freopen("data.out","w", stdout);
    
        clock_t  start_t,stop_t;//计时的时钟变量
        double dur_t;//中间计时
    
        start_t = clock();
        /*
        time():
            @function:获取当前的系统时间
            @return:time_t(本质:大整数)
            @significance:其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数.
        */
        printf("[LOCAL] Test LOCAL Define.
    ");
    #endif // LOCAL
    
        int x;
        while(scanf("%d", &x) == 1){//从文件内读取成功一个变量值
            printf("%d
    ", x);//写入数据
        }
        printf("Hello world!");
    
    #ifdef LOCAL
        stop_t = clock();
        dur_t = (double)(stop_t - start_t);
    
        printf("
    [TIME] start_t:%f", start_t);
        printf("
    [TIME] stop_t:%f", stop_t);
        printf("
    [TIME] during time:%f", dur_t);
    
        printf("
    [TIME] USE TIME:%ds", dur_t / CLOCKS_PER_SEC);
        /*
            常量CLOCKS_PER_SEC:表示一秒钟会有多少个时钟计时单元
        */
    #endif // LOCAL
        return 0;
    }
    
     

     

    /*
        数据统计与IO 非重定向版模板
    
        描述:若竞赛方要求使用文件输入输出,但禁止使用重定向方式时,可以采用此法。
    
        使用的函数与数据结构类型:
            FILE;
            fopen;
            fscanf;
            fprintf;
            fclose
        补充:
            若想把fopen版本的程序改成读写标准输入输出,只需要赋值:fin = stdin;fout = stdout即可
    
    */
    #include<stdio.h>
    #include<iostream>
    
    int main(){
        FILE *fin, *fout;
        fin = fopen("data.in", "rb");
        fout = fopen("data.out", "wb");
    
        int x;
        while(fscanf(fin, "%d", &x) == 1){//从文件内读取成功一个变量值
            fprintf(fout, "%d
    ", x);//写入数据
        }
    
        fprintf(fout, "%s", "[Test] Hello world!");
        fclose(fin);
        fclose(fout);
    
        return 0;
    }
    /*
    FILE * fopen(const char * path, const char * mode);
    返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL,并把错误代码存在 error 中。
    
    mode:
    r	以只读方式打开文件,该文件必须存在。
    r+	以读/写方式打开文件,该文件必须存在。
    rb+	以读/写方式打开一个二进制文件,只允许读/写数据。
    rt+	以读/写方式打开一个文本文件,允许读和写。
    w	打开只写文件,若文件存在则长度清为 0,即该文件内容消失,若不存在则创建该文件。
    w+	打开可读/写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
    a	以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留(EOF 符保留)。
    a+	以附加方式打开可读/写的文件。若文件不存在,则会建立该文件,如果文件存在,则写入的数据会被加到文件尾后,即文件原先的内容会被保留(原来的 EOF 符不保留)。
    wb	以只写方式打开或新建一个二进制文件,只允许写数据。
    wb+	以读/写方式打开或建立一个二进制文件,允许读和写。
    wt+	以读/写方式打开或建立一个文本文件,允许读写。
    at+	以读/写方式打开一个文本文件,允许读或在文本末追加数据。
    ab+	以读/写方式打开一个二进制文件,允许读或在文件末追加数据。
    
    */
    

      

     

    【参考文献】

      刘汝佳.算法竞赛入门经典 

  • 相关阅读:
    ecshop /includes/lib_base.php、/includes/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php Backdoor Vul
    ecshop /pick_out.php SQL Injection Vul By Local Variable Overriding
    dedecms /include/filter.inc.php Local Variable Overriding
    dedecms plusguestbook.php SQL Injection Vul By plusguestbookedit.inc.php
    帝国备份王(Empirebak) classfunctions.php、classcombakfun.php GETSHELL vul
    dedecms /member/uploads_edit.php SQL Injection Vul
    PHP Lex Engine Sourcecode Analysis(undone)
    dedecms /member/resetpassword.php SQL Injection Vul
    dedecms /member/reg_new.php SQL Injection Vul
    dedecms /member/pm.php SQL Injection Vul
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/9066183.html
Copyright © 2011-2022 走看看