MCS-201 · Unit 8

File Handling

Section 1 of 8

Block 2 · Unit 8File Handling10 min · Section 1 of 8

8.1Files, Streams and File Pointers

Covers IGNOU MCS-201 §8.0, 8.2

Before learning any functions, you need three ideas: what a file is to a C program, what a stream is, and how text files differ from binary files.

Why files

Data in variables is lost when the program exits. A file is a named collection of bytes stored on a disk (or other permanent storage), managed by the operating system. Writing data to a file keeps it after the program ends, and lets other programs, or later runs of the same program, read it back.

Streams

C does not talk to disks directly. It treats every source or destination of data as a stream: a sequence of bytes that can be read or written in order. A stream can be connected to a file, the keyboard or the screen, and the same functions work for all of them.

Three streams are opened automatically when a program starts:

StreamPointerNormally connected to
Standard inputstdinKeyboard
Standard outputstdoutScreen
Standard errorstderrScreen (used for error messages)

This is why printf("Hi") and fprintf(stdout, "Hi") do the same thing.

The FILE pointer

<stdio.h> defines a structure type called FILE. When a file is opened, the library creates a FILE object that records everything needed to work with it: the current position, the buffer, error and end-of-file indicators, and the operating system’s number for the open file (the file descriptor). The program never looks inside this object; it just keeps a pointer to it.

Declaring a file pointer
FILE *fp;

Buffering

The standard file functions are buffered: data is collected in a block of memory (a buffer) and moved to or from the disk in large pieces, because a disk access is far slower than a memory access. This is why data written to a file may not appear on disk until the buffer is flushed, for example by fclose.

Text files and binary files

Text fileBinary file
ContentsHuman-readable charactersThe bytes of data exactly as stored in memory
The integer 12345677 characters: '1' … '7' (7 bytes)One int: 4 bytes
Can be opened in an editorYesShows unreadable symbols
Newline handlingOn Windows, '\n' may be stored as carriage return + line feed; on Linux and macOS no changeNo translation ever
Typical functionsfprintf, fscanf, fgets, fputsfwrite, fread
PortabilityReadable on any systemDepends on sizes and byte order of the machine

Key takeaways

  • Files keep data after the program ends.
  • C reads and writes files through streams, referred to by FILE * pointers.
  • stdin, stdout and stderr are opened automatically.
  • Text files hold characters; binary files hold memory images of the data.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which stream is normally used for error messages?

Show answer

Answer: C. stderr

stderr is the standard error stream, kept separate from stdout so errors are visible even when normal output is redirected to a file.

Question 2 · multiple choice

How many bytes does the number 2026 take when written to a text file with fprintf(fp, "%d", 2026);?

Show answer

Answer: A. 4

Four digit characters are written, one byte each. No '\0' is written to the file.

Question 3 · multiple choice

What does a variable declared as FILE *fp; hold after a successful fopen?

Show answer

Answer: D. The address of a FILE object describing the open stream

fopen creates a FILE object with the stream’s details and returns a pointer to it; the data stays in the file.

Question 4 · multiple choice

Why are the standard file functions called buffered?

Show answer

Answer: B. Data is collected in memory and moved to or from the disk in large blocks

Buffering reduces the number of slow disk accesses by transferring data in blocks.

Question 5 · multiple choice

What does this program print?

C · stdout_demo.c
#include <stdio.h>

int main(void)
{
    fprintf(stdout, "%d-", 10);
    printf("%d\n", 20);
    return 0;
}
Show answer

Answer: D. 10-20

stdout is already open, so fprintf(stdout, …) prints to the screen just like printf.

Question 6 · short answer

Give two reasons why a program would store records in a binary file rather than a text file.

Show worked answer

First, a binary file stores each value in its fixed internal size (for example 4 bytes for an int), so every record has the same length and can be located directly by its position. Second, no conversion between numbers and characters is needed when reading or writing, so fread and fwrite are faster and simpler than formatting each field. The cost is that the file cannot be read in a text editor and may not be portable between different machines.

↑ ↓ move↵ openTab switch type