MCS-201 · Unit 8

File Handling

Section 8 of 8

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.

Buffered versus unbuffered

Buffered (standard C)Unbuffered (low-level, POSIX)
HandleFILE *int file descriptor
Headers<stdio.h><fcntl.h> (open, creat, O_ flags) and <unistd.h> (read, write, lseek, close)
BufferProvided by the librarySupplied by the programmer; each call is a system call
Functionsfopen, fclose, fread, fwrite, fseekopen, close, read, write, lseek
Formatted I/OYes (fprintf, fscanf)No, only raw bytes
Part of standard CYes, on every systemNo, POSIX (Linux, macOS); Windows has similar _open etc. in <io.h>

The functions

FunctionPrototype (POSIX)Returns
openint open(const char *path, int flags, ... /* mode */);A file descriptor, or −1 on error
creatint creat(const char *path, mode_t mode);A descriptor opened for writing (file created or truncated), or −1
readssize_t read(int fd, void *buf, size_t n);Bytes read; 0 at end of file; −1 on error
writessize_t write(int fd, const void *buf, size_t n);Bytes written, or −1
lseekoff_t lseek(int fd, off_t offset, int whence);New position, or −1
closeint 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 1 · multiple choice

What does read return when the end of the file is reached?

Show answer

Answer: C. 0

0 bytes read means end of file; −1 means an error. EOF belongs to the buffered functions.

Question 2 · multiple choice

Which value identifies an open file in low-level I/O?

Show answer

Answer: B. An integer file descriptor

open returns a small non-negative integer that the other low-level calls use to refer to the file.

Question 3 · multiple choice

Which open flags create a file if it is missing and empty it if it exists, for writing?

Show answer

Answer: D. O_WRONLY | O_CREAT | O_TRUNC

O_WRONLY opens for writing, O_CREAT creates the file and O_TRUNC empties an existing one; this is what creat does.

Question 4 · multiple choice

A source file has 170 bytes and is copied with while ((n = read(fd1, buf, 80)) > 0) write(fd2, buf, 80);. How many bytes does the copy contain?

Show answer

Answer: B. 240

read returns 80, 80 and then 10, but write always writes 80, so three blocks of 80 bytes (240 in total) are written, 70 of them garbage. Writing n bytes gives the correct 170.

Question 5 · multiple choice

Which file descriptor normally refers to standard output?

Show answer

Answer: B. 1

0 is standard input, 1 standard output and 2 standard error; files opened by the program usually get 3 and above.

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.

↑ ↓ move↵ openTab switch type