C Formatted File I/O

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Formatted File I/O?

fprintf writes values as text; fscanf reads them back using the same format. fscanf returns the number of items successfully read, which you should compare to the expected count. This is a simple way to store records such as name-score pairs.

Remember
fprintf(fp, "%s %d\n", name, score);

char name[32]; int score;
while (fscanf(fp, "%31s %d", name, &score) == 2) {
    /* use name and score */
}

Cheat Sheet

Keep these facts handy while you work with Formatted File I/O in C.

TopicDetail
Writefprintf(fp, fmt, ...)
Readfscanf(fp, fmt, &vars...)
ReturnItems matched, or EOF
StringsUse a width: %31s

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Binary Files.

Example 1 — Save and load name/score records

Compute an average after reading them back.

C
#include <stdio.h>

int main(void) {
    FILE *fp = fopen("scores.txt", "w");
    if (!fp) return 1;
    fprintf(fp, "%s %d\n", "Ava", 90);
    fprintf(fp, "%s %d\n", "Ben", 85);
    fclose(fp);

    char name[32];
    int score, total = 0, count = 0;
    fp = fopen("scores.txt", "r");
    if (!fp) return 1;
    while (fscanf(fp, "%31s %d", name, &score) == 2) {
        printf("%s: %d\n", name, score);
        total += score;
        count++;
    }
    fclose(fp);
    printf("Average: %.1f\n", (double)total / count);
    return 0;
}

How It Works

  1. Each record is one line: a name and a score.
  2. fscanf(...) == 2 stops at the end of the file or a malformed line.
  3. (90 + 85) / 2 = 87.5.

Edge Cases & Pitfalls

Check these before relying on Formatted File I/O in real programs.

width

Unbounded %s

%s without a width can overflow the buffer.

space

Names with spaces

%s stops at whitespace. Use fgets and parse manually for multi-word fields.

Key Takeaways

  • Symmetry: same format to write and read.
  • Check: fscanf return value.
  • Width: protect string buffers.

One line: fprintf writes values as text; fscanf reads them back using the same format.

Frequently Asked Questions

The number of input items successfully matched and assigned, or EOF if input ends before the first conversion.
It is fine for simple, well-formed data. For untrusted or irregular input, read lines with fgets and parse with strtol or sscanf.
Continue with "C Binary Files" next. Store raw data with fwrite and fread in "rb" and "wb" modes. The C File Handling hub lists every topic in order.

Next: Binary Files

Store raw data with fwrite and fread in "rb" and "wb" modes.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful