C Notes
Complete introduction to file handling in C programming — what are files, why file I/O is needed, types of files (text vs binary), file operations overview, streams, FILE pointer concept with examples.
Every program you've written so far loses its data the moment it exits. Variables live in RAM — they vanish when the program terminates. To store data permanently (between program runs), you need files. File handling in C gives you the power to create, read, write, and modify data stored on disk.
Why Do We Need File Handling?
Consider a student management system:
- Without files: You enter 100 students, program exits, all data is lost
- With files: Data is saved to disk and loaded back next time
| Scenario | Without Files | With Files |
|---|---|---|
| Data persistence | Lost on exit | Saved permanently |
| Large data sets | Limited by RAM | Limited by disk |
| Data sharing | Not possible | Save and share files |
| Logging | Console only | Persistent log files |
Types of Files in C
Text Files
- Contain human-readable characters (letters, digits, symbols)
- Stored as sequences of characters with newline translations
- Extension examples:
.txt,.csv,.c,.html - Can open in any text editor
Binary Files
- Store data in the same format as memory (raw bytes)
- Not human-readable when opened in a text editor
- More efficient for complex data structures
- Extension examples:
.bin,.dat,.exe,.png
| 101 Alice 3.85 | 65 00 00 00 41 6C 69 63 65 00 | |
|---|---|---|
| 102 Bob 3.65 | ... (raw bytes) ... | |
| (human readable) | (not human readable) |
The FILE Pointer
In C, all file operations work through a FILE pointer — an opaque structure that holds information about an open file:
FILE *fp; // File pointer declarationThe FILE structure (defined in <stdio.h>) internally contains:
- Buffer address and size
- Current position in the file
- End-of-file indicator
- Error indicator
- File descriptor (OS-level handle)
You never access these fields directly — you use library functions.
File Operations Overview
| Open | ───→ | Read / Write | ───→ | Close |
|---|---|---|---|---|
| fopen() | fread/fwrite | fclose() | ||
| fscanf/fprintf |
Every file operation follows three steps:
- Open the file (
fopen) - Perform operations (read/write)
- Close the file (
fclose)
Basic File Handling Example
Data written to hello.txt successfully. Contents of hello.txt: Hello, File Handling! This data persists on disk.
Standard Streams — Files You Already Use
C provides three pre-opened file streams:
| Stream | Name | Purpose | Connected To |
|---|---|---|---|
stdin | Standard Input | Read input | Keyboard |
stdout | Standard Output | Write output | Screen |
stderr | Standard Error | Write errors | Screen |
// These are equivalent:
printf("Hello\n");
fprintf(stdout, "Hello\n");
// Writing to stderr:
fprintf(stderr, "Error: something went wrong\n");Text Mode vs Binary Mode
// Text mode (default)
FILE *fp1 = fopen("data.txt", "r"); // Read text
FILE *fp2 = fopen("data.txt", "w"); // Write text
// Binary mode
FILE *fp3 = fopen("data.bin", "rb"); // Read binary
FILE *fp4 = fopen("data.bin", "wb"); // Write binary| Aspect | Text Mode | Binary Mode |
|---|---|---|
| Newlines | Translated (\n ↔ \r\n on Windows) | No translation |
| EOF marker | Ctrl+Z stops reading (Windows) | Read all bytes |
| Use for | .txt, .csv, source code | Images, audio, structs |
File Handling Functions Summary
| Function | Purpose | Header |
|---|---|---|
fopen() | Open a file | <stdio.h> |
fclose() | Close a file | <stdio.h> |
fgetc() | Read one character | <stdio.h> |
fputc() | Write one character | <stdio.h> |
fgets() | Read a line (string) | <stdio.h> |
fputs() | Write a string | <stdio.h> |
fscanf() | Formatted read | <stdio.h> |
fprintf() | Formatted write | <stdio.h> |
fread() | Read binary block | <stdio.h> |
fwrite() | Write binary block | <stdio.h> |
fseek() | Move file position | <stdio.h> |
ftell() | Get current position | <stdio.h> |
rewind() | Reset to beginning | <stdio.h> |
feof() | Check end-of-file | <stdio.h> |
ferror() | Check for errors | <stdio.h> |
Interview Questions
Q1: What is a FILE pointer in C?
A FILE pointer is a pointer to a structure (defined in stdio.h) that contains information about an open file — its buffer, current position, error status, and OS-level file descriptor. You use it as a handle for all file operations.
Q2: What's the difference between text mode and binary mode?
In text mode, newline characters are translated between the OS format (\r\n on Windows) and C's \n. Binary mode performs no translation — bytes are read/written exactly as they are. Use text for human-readable files, binary for data structures and media.
Q3: What happens if you forget to call fclose()?
Data in the output buffer may not be written to disk (data loss). File locks may not be released (other programs can't access the file). System resources (file descriptors) remain allocated. In long-running programs, you can run out of file descriptors.
Q4: What are stdin, stdout, and stderr?
They are pre-opened FILE* streams. stdin reads from the keyboard (or piped input), stdout writes to the screen (or piped output), stderr writes error messages to the screen (not affected by pipe redirection). You can use fprintf with any of them.
Summary
- File handling enables persistent data storage beyond program execution
- C uses FILE pointers as handles for file operations
- Always follow the pattern: open → read/write → close
- Text files store human-readable characters; binary files store raw bytes
- Always check if fopen() returns NULL before proceeding
- stdin, stdout, stderr are pre-opened file streams available without fopen
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for File Handling in C – Introduction.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, file, handling, introduction, file handling in c – introduction
Related C Programming Topics