---
title: "File I/O operations, errors, random access"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-io-errors-random-access"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-io-errors-random-access
site: BCT Engineering
---

# File I/O operations, errors, random access

Computer Programming · Chapter 10: Data Files

- Asked in: 2067 Asoj, 6+4 marks
- Video: [ftell() ||  fseek() || rewind() || Random Access Files In C programming ||  File Handling In C](https://www.youtube.com/watch?v=CC1bafI52ZQ) — Sudhakar Atchala
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-io-errors-random-access
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Explain the I/O operations on file with suitable example and also state the typical error situations during I/O operations in file. How the contents in the file can be randomly accessed?

## Short answer

File I/O: open with fopen, read/write via functions like fprintf/fscanf, and close. Typical errors: fopen returns NULL if missing, permissions wrong; disk full; past EOF; handle by checking NULL, return values, feof/ferror. Random access uses fseek(fp, offset, SEEK_SET/CUR/EEND), ftell, rewind; for fixed-size records, fseek to (n-1)*sizeof(record) to reach nth record.

## Answer

#### I/O operations on files
A file is processed in the steps: declare `FILE *fp;`, open with `fopen()`, read/write, close with `fclose()`. The main functions are `fprintf`/`fscanf` (formatted), `fgetc`/`fputc` (character), `fgets`/`fputs` (string) and `fread`/`fwrite` (blocks).

```c
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) { printf("Error\n"); return 1; }
fprintf(fp, "%d %s\n", 12, "Ram");
fclose(fp);
fp = fopen("data.txt", "r");
fscanf(fp, "%d %s", &roll, name);   /* read it back */
fclose(fp);
```

#### Typical error situations
1. Opening a file for reading that does not exist (`fopen` returns `NULL`).
2. Opening a file with a wrong mode, or without permission (e.g. read-only file or protected folder).
3. Disk full or write-protected media while writing.
4. Reading beyond the end of the file (EOF) without checking.
5. Using a file that was never opened or has already been closed.
6. Too many files open at the same time; invalid file name or path.

These are handled by testing the file pointer for `NULL`, testing the return values of `fscanf`/`fread`/`fwrite`, and using `feof()` and `ferror()`.

#### Random access
Normally a file is read from the start in sequence. The following functions move the file position:
- `fseek(fp, offset, origin)`: moves to `offset` bytes from `origin` (`SEEK_SET` start, `SEEK_CUR` current, `SEEK_END` end).
- `ftell(fp)`: returns the current position.
- `rewind(fp)`: goes back to the start.

For records of equal size, the n-th record is reached directly by `fseek(fp, (n-1)*sizeof(rec), SEEK_SET);`.

```c
#include <stdio.h>

int main(void)
{
    FILE *fp = fopen("rand.txt", "w+");
    if (fp == NULL) { printf("Cannot open\n"); return 1; }
    fputs("ABCDEFGHIJ", fp);

    fseek(fp, 3, SEEK_SET);                    /* 4th character */
    printf("At offset 3 : %c\n", fgetc(fp));
    printf("ftell       : %ld\n", ftell(fp));
    fseek(fp, -2, SEEK_END);                   /* 2nd from the end */
    printf("From the end: %c\n", fgetc(fp));
    rewind(fp);
    printf("After rewind: %c\n", fgetc(fp));
    fclose(fp);
    return 0;
}
```

Output:

```text
At offset 3 : D
ftell       : 4
From the end: I
After rewind: A
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the correct sequence for processing a file?
   A. fclose, fopen, read/write
   B. Declare FILE *fp, fopen, read/write, fclose
   C. fopen, fclose, read
   D. Read/write, then fopen
   Answer: B. A file pointer is declared, the file opened, processed and then closed. (Key point: Steps of file processing)

2. Which is a typical error situation?
   A. Opening an existing file for reading
   B. Opening a non-existent file for reading
   C. Closing an open file
   D. Writing to a file opened in w mode
   Answer: B. fopen returns NULL when a file for reading does not exist. (Key point: Error situation)

3. Which function moves to a given byte offset from SEEK_SET, SEEK_CUR or SEEK_END?
   A. ftell
   B. rewind
   C. fseek
   D. feof
   Answer: C. fseek(fp, offset, origin) repositions the file position. (Key point: Random access function)

4. After fseek(fp, 3, SEEK_SET) and reading one character, what does ftell return?
   A. 3
   B. 4
   C. 0
   D. 10
   Answer: B. Reading advances the position by 1, giving 4. (Key point: ftell result)

## Related in Computer Programming

- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Random access file transaction processing](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Arandom-access-transaction) — Object Oriented Programming, chapter 8
- [Errors encountered during file operation](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-errors) — Object Oriented Programming, chapter 8
- [Linked list has no random access](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinked-list-no-random-access) — Data Structure and Algorithm, chapter 4
- [File allocation and access methods](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afile-allocation-access-methods) — Operating System, chapter 5
