Block 2 · Unit 8File Handling12 min · Section 8 of 8
8.8Unbuffered (Low-Level) File I/O
Covers IGNOU MCS-201 §8.6
Underneath fopen, fread and friends, the operating system provides simpler routines that move bytes between a file and memory with no library buffer. These come from Unix and are standardised by POSIX; they are available on Linux and macOS, and in a similar form on Windows.
File descriptors
The low-level routines do not use FILE *. Instead, an open file is identified by a file descriptor: a small non-negative integer that indexes the operating system’s table of open files for the process. Descriptors 0, 1 and 2 are standard input, output and error.
Supplied by the programmer; each call is a system call
Functions
fopen, fclose, fread, fwrite, fseek
open, close, read, write, lseek
Formatted I/O
Yes (fprintf, fscanf)
No, only raw bytes
Part of standard C
Yes, on every system
No, POSIX (Linux, macOS); Windows has similar _open etc. in <io.h>
The functions
Function
Prototype (POSIX)
Returns
open
int open(const char *path, int flags, ... /* mode */);
A file descriptor, or −1 on error
creat
int creat(const char *path, mode_t mode);
A descriptor opened for writing (file created or truncated), or −1
read
ssize_t read(int fd, void *buf, size_t n);
Bytes read; 0 at end of file; −1 on error
write
ssize_t write(int fd, const void *buf, size_t n);
Bytes written, or −1
lseek
off_t lseek(int fd, off_t offset, int whence);
New position, or −1
close
int close(int fd);
0, or −1 on error
Common flags for open: O_RDONLY, O_WRONLY or O_RDWR, optionally combined with | and O_CREAT (create if missing), O_TRUNC (empty the file) or O_APPEND. When O_CREAT is used, the third argument gives the permissions of the new file, for example 0644 (owner may read and write; everyone else may read). lseek uses the same SEEK_SET, SEEK_CUR and SEEK_END as fseek.
Key takeaways
Low-level I/O identifies files by integer descriptors (0, 1 and 2 are the standard streams).
open/creat return a descriptor or −1; read returns bytes read, 0 at end of file.
Headers on Unix-like systems: <fcntl.h> and <unistd.h>.
Buffered standard I/O is portable and usually faster for small operations; low-level I/O gives direct control.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Give two differences between fopen/fread and open/read, and say which you would choose for reading a text file of numbers.
Show worked answer
First, fopen returns a FILE * and the library manages a buffer, while open returns an integer descriptor and every read is a separate system call with a buffer supplied by the programmer. Second, the buffered functions are part of standard C and work on every system, and include formatted functions such as fscanf; the low-level functions are POSIX and transfer only raw bytes. For a text file of numbers, the buffered functions are the better choice, because fscanf converts the text to numbers directly.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/file-handling/unbuffered-low-level-file-i-o/ to read this page.