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:
Stream
Pointer
Normally connected to
Standard input
stdin
Keyboard
Standard output
stdout
Screen
Standard error
stderr
Screen (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 file
Binary file
Contents
Human-readable characters
The bytes of data exactly as stored in memory
The integer 1234567
7 characters: '1' … '7' (7 bytes)
One int: 4 bytes
Can be opened in an editor
Yes
Shows unreadable symbols
Newline handling
On Windows, '\n' may be stored as carriage return + line feed; on Linux and macOS no change
No translation ever
Typical functions
fprintf, fscanf, fgets, fputs
fwrite, fread
Portability
Readable on any system
Depends 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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/file-handling/files-streams-and-file-pointers/ to read this page.