Update Input_Output.md

This commit is contained in:
Anthony Wang 2020-06-18 14:19:44 -05:00 committed by GitHub
parent 8e697025cb
commit 900dd09cd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,7 @@ Demonstrates how to read input and print output for USACO.
In CodeForces and CSES, input and output are **standard**, meaning that using the library [\<iostream\>](http://www.cplusplus.com/reference/iostream/) suffices.
However, in USACO, input is read from a file called `problemname.in`, and printing output to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. You will need the [\<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [\<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word *template* in the word below with the input/output file name, which should be given in the problem.
However, in USACO, input is read from a file called `problemname.in`, and printing output to a file called `problemname.out`. Note that you'll have to rename the `.in` and `.out` files. You will need the [\<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [\<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem. For example, for (this problem)[http://www.usaco.org/index.php?page=viewproblem2&cpid=1035], you would replace `problemname` with `socdist1` to get `socdist1.in` and `socdist1.out`.
In order to test a program, create a file called `problemname.in`, and then run the program. The output will be printed to `problemname.out`.
@ -28,12 +28,14 @@ If `<cstdio>` is used:
using namespace std;
int main() {
freopen("template.in", "r", stdin);
freopen("template.out", "w", stdout);
freopen("problemname.in", "r", stdin);
freopen("problemname.out", "w", stdout);
// rest of your code ...
}
```
If `<fstream>` is used (note that if you use `<fstream>`, you must replace `cin` and `cout` with `fin` and `fout`):
If `<fstream>` is used (Note that you cannot use C-style I/O (scanf, prinf) with this method):
```cpp
#include <fstream>
@ -41,8 +43,10 @@ If `<fstream>` is used (note that if you use `<fstream>`, you must replace `cin`
using namespace std;
int main() {
ifstream fin("template.in");
ofstream fout("template.out");
ifstream cin("problemname.in");
ofstream cout("problemname.out");
// rest of your code ...
}
```
@ -180,4 +184,4 @@ public static void main(String[] args) {
pw.println(a + b + c);
pw.close();
}
```
```