How to Read a file in C

Reading files in C is very easy. First you need the stdio.h header file included, then you need a pointer of type FILE, and you call fopen on a filename (setting a flag to tell what access you want - here we just want read access).

Last you make a buffer, and then loop through the files data, read data into the buffer, and the printing (or otherwise use) the data.

#include <stdio.h>
int main(int argc, char **argv)
  FILE *f;

  f = fopen("read.c", "r");
  char buf[BUFSIZ];
  while(NULL != fgets(buf, sizeof buf, f)) {
    printf("%s", buf);