| ... | ... | @@ -24,7 +24,7 @@ Xcode will allow you specify command-line parameters, but no longer directly sup |
|
|
|
|
|
|
|
#### Examples
|
|
|
|
|
|
|
|
```C++
|
|
|
|
```c++
|
|
|
|
int main() {
|
|
|
|
freopen("redirected-to-cin.txt", "r", stdin); // Redirected input
|
|
|
|
freopen("redirected-from-cout.txt", "w", stdout); // Redirected output
|
| ... | ... | @@ -40,7 +40,7 @@ How to do it well |
|
|
|
|
|
|
|
You have seen compiler guards before in header files in the form of
|
|
|
|
|
|
|
|
```
|
|
|
|
```c++
|
|
|
|
#ifndef VAR
|
|
|
|
#define VAR
|
|
|
|
...
|
| ... | ... | @@ -48,10 +48,10 @@ You have seen compiler guards before in header files in the form of |
|
|
|
```
|
|
|
|
You can read more about these [here](https://msdn.microsoft.com/en-us/library/ew2hz0yd.aspx). On macOS, the operating system macro is `__APPLE__`.
|
|
|
|
|
|
|
|
```
|
|
|
|
```c++
|
|
|
|
#ifdef __APPLE__
|
|
|
|
freopen(“inputFilename.txt”, “r”, stdin);
|
|
|
|
freopen(“outputFilename.txt”, “w”, stdout);
|
|
|
|
freopen("inputFilename.txt", "r", stdin);
|
|
|
|
freopen("outputFilename.txt", "w", stdout);
|
|
|
|
#endif
|
|
|
|
```
|
|
|
|
|
| ... | ... | @@ -61,7 +61,7 @@ This will compile with `freopen()` *only* when the operating system is *macOS*, |
|
|
|
|
|
|
|
Another solution is to include \<stdlib.h\> (this is important!) and use `getenv()`, which lives in stdlib.h.
|
|
|
|
|
|
|
|
```
|
|
|
|
```c++
|
|
|
|
if (getenv("INPUT")) {
|
|
|
|
freopen(getenv("INPUT"), "r", stdin);
|
|
|
|
freopen(getenv("OUTPUT"), "w", stdout);
|
| ... | ... | |