Line Filters in C
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in C that writes a capitalized version of all input text. You can use this pattern to write your own C line filters.
This program uses fgets()
to read input line by line from stdin. Each line is then capitalized using a custom capitalize()
function, which uses the toupper()
function from ctype.h
. The capitalized line is then printed to stdout using printf()
.
To try out our line filter, first make a file with a few lowercase lines.
Then compile and use the line filter to get uppercase lines.
This C version achieves the same functionality as the original program. It reads input line by line, capitalizes each line, and prints the result. Error checking is done at the end of the program to ensure all input was read successfully.