---
title: "I/O functions used with data files"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-io-functions"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-io-functions
site: BCT Engineering
---

# I/O functions used with data files

Computer Programming · Chapter 10: Data Files

- Asked in: 2074 Asoj, 4 marks; 2068 Shrawan, 4 marks (asked 2 times)
- Video: [#29: C File Handling | [2025] C Programming for Beginners](https://www.youtube.com/watch?v=MQIF-WMUOL8) — Programiz
- 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-functions
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

What are different input/output functions used with data files in C? Explain with syntax and examples. (Explain fscanf, fprintf, fread, fwrite functions.)

## Short answer

fprintf and fscanf perform formatted text I/O on files. fwrite and fread transfer binary blocks of data directly between memory and file, useful for structures. They require a file pointer opened with fopen and closed with fclose.

## Answer

Data files are accessed through a file pointer (`FILE *fp`) opened with `fopen()`. The main input/output functions (all in `<stdio.h>`) are:

| Function | Purpose | Syntax |
|---|---|---|
| `fprintf` | Write formatted text | `fprintf(fp, "format", list);` |
| `fscanf` | Read formatted text | `fscanf(fp, "format", &list);` |
| `fputc` / `fgetc` | Write / read one character | `fputc(ch, fp);` `ch = fgetc(fp);` |
| `fputs` / `fgets` | Write / read a string | `fputs(str, fp);` `fgets(str, n, fp);` |
| `fwrite` | Write blocks (binary) | `fwrite(ptr, size, count, fp);` |
| `fread` | Read blocks (binary) | `fread(ptr, size, count, fp);` |

- `fprintf` and `fscanf` work like `printf` and `scanf`, but on a file. `fscanf` returns the number of items read (or EOF).
- `fwrite` and `fread` copy `count` blocks of `size` bytes directly between memory and the file, so a whole structure is stored in one call. Here `ptr` is the address of the data, for example `&s` for a structure variable `s`. They return the number of blocks transferred.

```c
#include <stdio.h>

struct rec {
    char name[20];
    int roll;
};

int main(void)
{
    FILE *fp;
    struct rec s = {"Ram", 7}, t;
    char name[20];
    int roll;

    /* formatted I/O on a text file */
    fp = fopen("text.txt", "w");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    fprintf(fp, "%s %d\n", "Sita", 12);
    fclose(fp);

    fp = fopen("text.txt", "r");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    fscanf(fp, "%s %d", name, &roll);
    printf("fscanf read: %s %d\n", name, roll);
    fclose(fp);

    /* block I/O on a binary file */
    fp = fopen("rec.dat", "wb");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    fwrite(&s, sizeof(s), 1, fp);
    fclose(fp);

    fp = fopen("rec.dat", "rb");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    fread(&t, sizeof(t), 1, fp);
    printf("fread read : %s %d\n", t.name, t.roll);
    fclose(fp);
    return 0;
}
```

Output:

```text
fscanf read: Sita 12
fread read : Ram 7
```

## Check yourself

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

1. Which pair is used for text (formatted) and binary block output respectively?
   A. fwrite and fprintf
   B. fprintf and fwrite
   C. fputs and fgetc
   D. fscanf and fread
   Answer: B. fprintf writes formatted text; fwrite copies blocks of bytes directly to the file. (Key point: fprintf vs fwrite)

2. In fread(&t, sizeof(t), 1, fp), what do the second and third arguments mean?
   A. Size of each block and number of blocks
   B. Number of blocks and size
   C. File mode and size
   D. Start and end positions
   Answer: A. count blocks of size bytes are read from the file into memory. (Key point: fread arguments)

3. What does fscanf return?
   A. Number of items read successfully, or EOF
   B. The file size
   C. Always 1
   D. The file pointer
   Answer: A. The return value tells how many items were read, which is used to detect the end or errors. (Key point: fscanf return)

4. Why are fwrite/fread convenient for records?
   A. They convert numbers to text
   B. A whole structure is stored or read in one call
   C. They need no file pointer
   D. They work only on characters
   Answer: B. They copy count blocks of size bytes directly between memory and the file. (Key point: Whole structure in one call)

## Related in Computer Programming

- [Formatted I/O in FORTRAN with syntax](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-formatted-io-syntax) — Computer Programming, chapter 11
- [Syntax of while loop](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-while) — Computer Programming, chapter 5
- [Syntax of if...else](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-if-else) — Computer Programming, chapter 5
- [Syntax of static](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-static) — Computer Programming, chapter 6
- [Syntax of struct](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-struct) — Computer Programming, chapter 8
- [Odd and even numbers to separate files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-odd-even-until-no) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [LTI output for h(t)=e^(-2t), rectangular input](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Alti-output-rect-input) — Data Communication, chapter 2, asked 3×
- [Data logger input scanner and conditioner design](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Adatalogger-three-transducers) — Instrumentation II, chapter 5
- [Output of e^(−2t)u(t) to a rectangular input](https://www.bctengineer.com/question.md?key=semester_4%2Fsignals_and_systems%3Aconvolution-exponential-rect-input) — Signals and Systems, chapter 5, asked 4×
- [CE configuration input/output characteristics](https://www.bctengineer.com/question.md?key=semester_2%2Fbasic_electronics_engineering%3Ace-npn-characteristics) — Basic Electronics Engineering, chapter 3, asked 3×
