### Rudimentary File IO, or How I Learnt to Lay Pipes
Enough theory, let's get down to business by writing some code!
The easist way to write to a file is to redirect the output stream using the output redirect tool, `>`.
If you want to append, you can use `>>`. _N.b. these redirection operators are in_`bash`_and similar shells._
```bash
# This will output to the screen...
./helloworld
# ...but this will write to a file!
./helloworld > hello.txt
```
The contents of `hello.txt` will, not surprisingly, be
```
Hello, world!
```
Say we have another program called `greet`, similar to `helloworld`, that greets you given your name.
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initialize an array to hold the name.
char name[20];
// Read a string and save it to name.
scanf("%s", name);
// Print the greeting.
printf("Hello, %s!", name);
return EXIT_SUCCESS;
}
```
Instead of reading from the keyboard, we can redirect `stdin` to read from a file using the `<` tool.
```bash
# Write a file containing a name.
echo Kamala > name.txt
# This will read the name from the file and print out the greeting to the screen.
./greet <name.txt
# ==> Hello, Kamala!
# If you wanted to also write the greeting to a file, you could do so using ">".
```
### The Real Deal
The above methods only worked for the most basic of cases. If you wanted to do bigger and better things, you will probably want to work with files from within C instead of through the shell.
To accomplish this, you will use a function called `fopen`. This function takes two string parameters, the first being the file name and the second being the mode.
Mode is basically permissions, so `r` for read, `w` for write, `a` for append. You can also combine them, so `rw` would mean you could read and write to the file. There are more modes, but these are the most used.
After you have a `FILE` pointer, you can use basically the same IO commands you would've used, except that you have to prefix them with `f` and the first argument will be the file pointer.
For example, `printf`'s file version is `fprintf`.
Here's a program called `greetings` that reads a from a file containing a list of names and writes to another file the greetings.
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
// Create file pointers.
FILE *names = fopen("names.txt", "r");
FILE *greet = fopen("greet.txt", "w");
// Check that everything is OK.
if (!names || !greet) {
fprintf(stderr, "File opening failed!\n");
return EXIT_FAILURE;
}
// Greetings time!
char name[20];
// Basically keep on reading untill there's nothing left.
while (fscanf(names, "%s\n", name) > 0) {
fprintf(greet, "Hello, %s!\n", name);
}
// When reached the end, print a message to the terminal to inform the user.