🎓 A1Learner
🔍
0 of 50 lessons completed

C Read Files

Writing fills a file, and reading brings the words back. fscanf reads formatted values, and fgets reads one whole line at a time.

Read two numbers with fscanf

Two scores go into scores.txt first, then the file is read back. The output shows 85 first and 92 second, so both numbers survived the round trip.

#include <stdio.h>

int main() {
  FILE *fp = fopen("scores.txt", "w");
  if (fp == NULL) {
    printf("Could not open scores.txt.\n");
    return 1;
  }
  fprintf(fp, "85\n");
  fprintf(fp, "92\n");
  fclose(fp);
  fp = fopen("scores.txt", "r");
  if (fp == NULL) {
    printf("Could not read scores.txt.\n");
    return 1;
  }
  int first = 0;
  int second = 0;
  fscanf(fp, "%d", &first);
  fscanf(fp, "%d", &second);
  fclose(fp);
  printf("First: %d\n", first);
  printf("Second: %d\n", second);
  return 0;
}

Read one line with fgets

One line goes into notes.txt first, then fgets reads it back whole. The output is the same line, spaces and all.

#include <stdio.h>

int main() {
  char line[50] = {0};
  FILE *fp = fopen("notes.txt", "w");
  if (fp == NULL) {
    printf("Could not open notes.txt.\n");
    return 1;
  }
  fprintf(fp, "Hello, file!\n");
  fclose(fp);
  fp = fopen("notes.txt", "r");
  if (fp == NULL) {
    printf("Could not read notes.txt.\n");
    return 1;
  }
  fgets(line, 50, fp);
  fclose(fp);
  printf("%s", line);
  return 0;
}

Read every line in a loop

Three lines go into diary.txt first, then a loop reads them back one at a time. The output shows all three lines in order.

#include <stdio.h>

int main() {
  char line[50] = {0};
  FILE *fp = fopen("diary.txt", "w");
  if (fp == NULL) {
    printf("Could not open diary.txt.\n");
    return 1;
  }
  fprintf(fp, "Day one.\n");
  fprintf(fp, "Day two.\n");
  fprintf(fp, "Day three.\n");
  fclose(fp);
  fp = fopen("diary.txt", "r");
  if (fp == NULL) {
    printf("Could not read diary.txt.\n");
    return 1;
  }
  while (fgets(line, 50, fp) != NULL) {
    printf("%s", line);
  }
  fclose(fp);
  return 0;
}

Example

#include <stdio.h>

int main() {
  char line[50] = {0};
  FILE *fp = fopen("grades.txt", "w");
  if (fp == NULL) {
    printf("Could not open grades.txt.\n");
    return 1;
  }
  fprintf(fp, "Ada: 88\n");
  fprintf(fp, "Ben: 76\n");
  fclose(fp);
  fp = fopen("grades.txt", "r");
  if (fp == NULL) {
    printf("Could not read grades.txt.\n");
    return 1;
  }
  while (fgets(line, 50, fp) != NULL) {
    printf("%s", line);
  }
  fclose(fp);
  printf("Done.\n");
  return 0;
}

Key points

  • Open a file in read mode with "r" to read from it.
  • fscanf reads values from a file the way scanf reads from the keyboard.
  • fscanf needs & before a variable, as scanf does.
  • fgets reads one whole line, spaces and all, into a char array.
  • fgets gives back NULL when there is no line left to read.
  • Read in a loop to take every line from a file.
  • Close the file after reading, as after writing.

Two graded lines go into grades.txt, then the file is read back line by line. The output shows both lines, then Done.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which call reads one whole line from a file into a char array named line?