C doesn’t make text handling complicated. It strips away the abstraction layers and leaves you with the raw mechanics. If you have been staring at C documentation, you probably noticed that all text input/output functions live inside the stdio library. This stands for standard input/output. It is the bedrock of how C programs talk to the outside world.

Most beginners start with stdin and stdout. These are standard streams. They represent where a program gets data and where it sends results. If your application only needs one source of input and one sink for output, you don’t need complex file management. You just pipe data through the command line.

The Six Basic Streams

The stdio header provides six specific commands for interacting with these streams. They are simple. Direct.

  • printf : Writes formatted text to stdout.
  • scanf : Reads formatted data from stdin.
  • puts : Outputs a string to the screen.
  • gets : Reads a line of text from the keyboard.
  • putc : Writes a single character to stdout.
  • getc or getchar : Reads a single character from stdin.

Why use these? Because you can redirect them. This is where C shines. You don’t need to change your code to read from a file instead of a keyboard. You just change the command you type in the terminal.

Counting Characters The Easy Way

Let’s look at a practical example. Imagine you want a program that counts the total characters in a text input. Here is the code:

This code waits for input. You type lines. When you are done, you press CTRL-D. This signals the end of file (eof ). The gets function returns zero when it hits eof, breaking the loop. The final count prints to the screen.

But here is the trick. You didn’t have to edit the code to count characters in a file. You just added a small instruction to the shell.

Redirecting Input And Output

If you compiled this program into an executable named xxx, you can feed it a file.

The angle bracket redirects the content of filename into stdin. The program thinks it is reading from the keyboard. It isn’t. It is reading from disk.

You can also use pipes. This passes the output of one command directly into another.

This is the same result. cat dumps the file to standard output. The pipe sends it to your program.

You can also redirect the output. Instead of seeing the count on your screen, you can save it.

Now xxx reads from filename and writes the result into a new text file named out. This is powerful. It allows you to chain tools together without writing extra code to handle file pointers.

When Streams Aren’t Enough

Sometimes redirection isn’t enough. Maybe you need to open multiple files at once. Maybe you are building a text editor that saves configuration files. Maybe you need to check if you have reached the end of a file without crashing.

In these cases, you need direct file functions. These are also in stdio, but they operate on file pointers rather than standard streams.

  • fopen : Opens a file for reading or writing.
  • fclose : Closes the file when you are done.
  • feof : Checks if you have hit the end of the file.
  • fprintf : Writes formatted data to a specific file.
  • fscanf : Reads formatted data from a specific file.
  • fputs : Writes a string to a file.
  • fgets : Reads a line from a file.
  • fputc : Writes a character to a file.
  • fgetc : Reads a character from a file.

These functions give you more control. You can manage several streams at once. You can decide exactly which file gets which data.

Which path do you take? The simplicity of stdin redirection or the control of direct file pointers? It depends on what you are building. For quick scripts, redirection is king. For complex applications, you will need the file functions.

There is no perfect answer. Just trade-offs.