File operations — open, read, write, seek, close
WHY do we need these operations at all?
The 5 core operations form a lifecycle:
open → (read / write / seek)* → close
open— set up the bookkeeping, get a handle.read/write— transfer bytes between your buffer and the file.seek— move where in the file the next read/write happens.close— tear down the bookkeeping, flush data.
WHAT exactly is a file descriptor?

HOW each call works (derived from first principles)
1. open
2. read
3. write
4. seek (lseek)
5. close
Worked examples
Common mistakes
Recall Feynman: explain to a 12-year-old
Imagine a library where you can't go inside. You ask the librarian (the OS) for a book by name — she gives you a numbered claim-ticket (the file descriptor). With that ticket you say "read me the next page" or "write this page" and she remembers which page you're on (the offset). If you say "skip to page 50," that's seek — she just flips a bookmark, super fast, no reading. When you're done you hand the ticket back (close) so someone else can use it.
Active recall
What does open return on success, and on failure?
-1 (with errno set) on failure.What does read returning 0 mean?
-1.Why might read/write transfer fewer bytes than requested?
What state does the open file description hold?
Compute lseek(fd, -4, SEEK_END) for a 20-byte file.
Does lseek transfer data or move the disk head?
Which flag makes every write go to end-of-file atomically?
O_APPEND.Which flags are needed to create a file if absent and what extra argument matters?
O_CREAT; the mode (e.g. 0644) permission argument is only used when creating.What happens if you never close fds in a long-running program?
open calls fail (ulimit/EMFILE).What is a sparse file and how is it produced?
Convention: what are fds 0, 1, 2?
Connections
- File system & inodes —
openresolves path → inode. - System calls and the kernel boundary — these are all syscalls.
- File descriptor table — per-process table the fd indexes.
- Buffered I/O (fopen/fread) — stdio wrappers built on top of these.
- Pipes and sockets — also use
read/writebut are non-seekable. - ulimit and resource limits — why closing matters.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, file disk pe ek simple byte ka array hoti hai — kuch magic nahi. Tum apne program se
disk ko directly touch nahi kar sakte, kyunki disk shared hardware hai aur OS uska gatekeeper hai.
Isliye tum OS ko bolte ho open("file"), aur OS tumhe ek chhota number deta hai — file descriptor (fd).
Yeh fd basically ek claim-ticket hai jisse aage ke saare kaam hote hain. fd 0,1,2 by default
stdin, stdout, stderr hote hain, aur naya open hamesha sabse chhota free number deta hai.
Ab fd ke peeche OS ek offset yaad rakhta hai — yani abhi tum file me kis byte pe ho. read
karoge to offset se aage ke bytes aate hain aur offset apne aap aage badh jaata hai. write ulta —
buffer se file me bytes likhte ho. Important baat: read/write jitna tumne maanga utna hi
return kare yeh zaroori nahi — short read/write hota hai, isliye hamesha return value check karo.
read ka 0 matlab EOF (file khatam), error nahi; error -1 hota hai. Yeh galti sabse common
hai exams aur interviews dono me.
seek (yani lseek) ek smart cheez hai: yeh data move nahi karta, sirf offset ka number badalta
hai — isliye yeh almost free hai, koi disk head physically move karne wali baat nahi. SEEK_SET,
SEEK_CUR, SEEK_END se tum absolute ya relative jump kar sakte ho — isi se random access milta
hai. Aur jab kaam ho jaaye to close zaroor karo, warna fd leak hoga aur ek time pe naye file open
hona band ho jaayenge (ulimit limit).
Yaad rakhne ka tareeka: Open → (Read/Write/Seek baar baar) → Close. Bookend do hi cheezein — open aur close — beech ka teen kuch bhi repeat ho sakta hai. Yeh lifecycle dimaag me fit ho gaya to file I/O ka 80% kaam clear hai.