Reading a File Line-by-Line


We often need to read some input file line-by-line and perform some operation on each line. For instance, our program may have to read a simple configuration file, or process a text file.

Basic template

The following program is a basic template for reading a file line-by-line in C. You will need to modify the function to perform whatever processing your program requires; here, we simply print the line and some additional information that getline returns.

The core function is read_lines, which uses the following libc library functions: