File Handling

File handling opens a stream in a chosen mode, reads or writes through it, and closes it to flush buffers and release the OS handle; the access mode determines whether the file is read, overwritten, or appended and whether it is text or binary.

Key formulas & points

Skim these first — then read the full notes below.

  • Modes: r, w, a, r+, and binary variants (rb, wb)
  • Buffering may be full, line or none — fflush forces a write
  • Always check the return value of fopen and fread

Topic details

Introduction

This topic covers persistent input and output. You learn to open files in the correct mode, read and write with the standard library functions, position within a file for random access, understand buffering, and handle errors such as a failed open or a short read.

Key relations & formulas

Formulas (Indian textbook notation)

  • fopen(path,mode)FILE;fclose(fp)releasesthehandlefopen(path, mode) → FILE*; fclose(fp) releases the handle

Formulas (Indian textbook notation)

  • fread(buf,size,count,fp)numberofitemsreadfread(buf, size, count, fp) → number of items read

Formulas (Indian textbook notation)

  • fseek(fp,offset,SEEKSET)randomaccesspositioningfseek(fp, offset, SEEK_{SET}) → random access positioning

Notation and sign conventions

Relation 1 —
fopenfopen

Formulas (Indian textbook notation)

  • fopen(path,mode)FILE;fclose(fp)releasesthehandlefopen(path, mode) → FILE*; fclose(fp) releases the handle
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
freadfread

Formulas (Indian textbook notation)

  • fread(buf,size,count,fp)numberofitemsreadfread(buf, size, count, fp) → number of items read
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
fseekfseek

Formulas (Indian textbook notation)

  • fseek(fp,offset,SEEKSET)randomaccesspositioningfseek(fp, offset, SEEK_{SET}) → random access positioning
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

A file stream is a buffered channel between the program and storage; the mode chosen at open time is a contract — "w" truncates any existing file, "a" appends, "r+" allows update. Buffering means writes may not hit the disk immediately, so fflush or fclose is needed to guarantee data is saved, which matters if the program might crash. Random access via fseek treats the file as an addressable sequence of bytes, essential for fixed-record formats. Robust code always checks return values because a file may be missing, permission-denied, or shorter than expected.

Assumptions and validity limits

State assumptions explicitly before using any relation for file handling — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Programming Fundamentals viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Programming Fundamentals papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to file handling.
4. Use equation 1:
fopenfopen
.
5. Use equation 2:
freadfread
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

File Handling appears in all software development. In Indian it software curricula this topic is tested because it connects theory to core programming concepts in C/C++.
GATE and semester exams often combine file handling with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use file handling?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students open with the wrong mode (using "w" and unexpectedly truncating data), forget to close files (leaking handles and losing buffered writes), and ignore return values so a failed open goes undetected. Mixing text and binary modes corrupts data on some platforms.

Quick revision checklist

Before attempting file handling problems, confirm you can:
1. Modes: r, w, a, r+, and binary variants (rb, wb)
2. Buffering may be full, line or none — fflush forces a write
3. Always check the return value of fopen and fread
Revise the solved examples in Let Us C — Yashavant Kanetkar and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Choosing a file mode

Problem

You must add log lines to an existing file without erasing its contents. Which fopen mode is correct?

Solution

Use append mode "a": it opens (or creates) the file and positions writes at the end, preserving existing content. Mode "w" would truncate the file to zero length.

Conceptual check — File Handling

Problem

In a Programming Fundamentals semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of file handling." What should a complete answer include?

📖 Standard books (India)

  • Let Us CYashavant Kanetkar

    Read: Syllabus unit

    First programming book for many Indian students