Tip 2-15: Under Windows, enter after the completion of the input key, and then press CTRL + Z, and finally press ENTER key, you can end the input. Under Linux, you can end the input by pressing the CTRL+D key when you have finished typing.
Two ways to work with files include: Freopen and fopen.
Freopen Way
The simplest way to use a file is to use input-output redirection, by simply adding the following two statements at the entrance of the main function:
Freopen ("input.txt""R", stdin); Freopen (" output.txt""w", stdout);
It allows scanf to read from the file Input.txt, and printf writes to the file output.txt. In fact, not just scanf and printf, all functions that read keyboard input and write screen output will use files instead. Although this is convenient, not all algorithmic contests allow you to read and write files using programs. There are even contests that allow access to files, but do not allow you to read and write files with freopen such as redirects.
There is a way to re-redirect the files in a native test, but once submitted to the game, the redirect statement is automatically "deleted". The code is as follows:
Program 2-8 Data Statistics (redirected version)
#defineLOCAL#include<stdio.h>#defineINF 1000000000intMain () {#ifdef LOCAL freopen ("data.in","R", stdin); Freopen ("Data.out","W", stdout); #endif //LOCAL intX, n =0, Min = INF, max =-inf, s =0; while(SCANF ("%d", &x)! =EOF) {s+=x; if(x < min) min =x; if(x > Max) max =x; N++; } printf ("%d%d%.3lf\n", Min, Max, (Double) s/N); return 0;}
The redirected parts were unloaded in #ifdef and #endif. It means that only the local is defined, and the two Freopen statements are compiled.
The above code defines local in the program header, so you can use redirection to read and write files when you test natively. If the match requires reading and writing standard input and output, simply delete the # define local before committing. A better approach is to define the local symbol in the compile option instead of the program (refer to the appendix for readers who do not know how to define the symbol in the compilation option), so that the program does not need to be modified before submission, further reducing the likelihood of errors.
Fopen Way
If the game requires file input but is not redirected, you can do the following:
Program 2-9 Data Statistics (version fopen)
#include <stdio.h>#defineINF 1000000000intMain () {FILE*fin, *Fout; Fin= fopen ("data.in","R"); Fout= fopen ("Data.out","W"); intX, n =0, Min = INF, max =-inf, s =0; while(FSCANF (FIN,"%d", &x)! =EOF) {s+=x; if(x < min) min =x; if(x > Max) max =x; N++; } fprintf (Fout,"%d%d%.3lf\n", Min, Max, (Double) s/N); Fclose (Fin); Fclose (Fout); return 0;}
There are two ways to redirect and fopen each have advantages and disadvantages. The redirection method is simple and natural to write, but cannot read and write files and standard input output at the same time; fopen is a bit cumbersome, but more flexible (for example, you can open and write files repeatedly). By the way, if you want to change the fopen version of the program to standard input and output, simply assign a value
Fin=stdin;fout=stdout;
, do not call fopen and fclose.
"Algorithmic Competition Primer" Learn Note 2.3 file operations