codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C read files

C read files

Read text from a file.

Open with "r" and read lines with fgets until it returns NULL.

Example code

#include <stdio.h>
int main() {
  char line[100];
  FILE *f = fopen("data.txt", "r");
  if (!f) { printf("Missing\n"); return 1; }
  while (fgets(line, 100, f)) printf("%s", line);
  fclose(f);
  return 0;
}

Run this code in the online editor

← C create and write filesC structures →