MCS-201 · Unit 8

File Handling

Section 6 of 8

Block 2 · Unit 8File Handling8 min · Section 6 of 8

8.6Block Input and Output

Covers IGNOU MCS-201 §8.3.4

When a program stores structures, such as student records, converting every field to text is slow and awkward. fwrite and fread copy whole blocks of memory to and from a file in one call.

fwrite and fread

Prototypes
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp);
size_t fread(void *ptr, size_t size, size_t count, FILE *fp);
ParameterMeaning
ptrAddress of the data to write, or of the memory that will receive the data read
sizeSize in bytes of one item, normally sizeof the variable or type
countNumber of items to transfer
fpThe file, normally opened in binary mode ("wb", "rb", "r+b")

Both return the number of items (not bytes) transferred. A return value smaller than count means the end of the file was reached or an error occurred, so while (fread(&rec, sizeof rec, 1, fp) == 1) reads records until the end.

Key takeaways

  • fwrite and fread transfer count items of size bytes each.
  • They return the number of complete items transferred.
  • Use binary modes ("wb", "rb", "r+b") for record files.
  • Read records with while (fread(...) == 1).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does fwrite(arr, sizeof(int), 10, fp) write on success?

Show answer

Answer: C. 10 integers, that is 10 × sizeof(int) bytes

The second argument is the size of one item and the third the number of items; with a 4-byte int this is 40 bytes.

Question 2 · multiple choice

What does fread return?

Show answer

Answer: B. The number of complete items read

Like fwrite, it counts items. At the end of the file it returns fewer items than requested (often 0), not EOF.

Question 3 · multiple choice

Which mode should be used to create a new file of structure records?

Show answer

Answer: D. "wb"

w creates (or empties) the file, and b selects binary mode, which stores the bytes without any newline translation.

Question 4 · multiple choice

A file contains exactly 4 records of type struct item. What does this fragment print?

C · count_records.c
#include <stdio.h>

struct item { int code; double price; };

int main(void)
{
    struct item it[4] = {{1, 2.5}, {2, 3.0}, {3, 1.5}, {4, 9.0}}, x;
    FILE *fp = fopen("items.dat", "wb");
    int n = 0;
    if (fp == NULL)
        return 1;
    fwrite(it, sizeof it[0], 4, fp);
    fclose(fp);
    fp = fopen("items.dat", "rb");
    if (fp == NULL)
        return 1;
    while (fread(&x, sizeof x, 1, fp) == 1)
        n++;
    fclose(fp);
    printf("%d\n", n);
    return 0;
}
Show answer

Answer: D. 4

Four records are written in one call and the loop reads each once; the fifth fread returns 0 and ends the loop.

Question 5 · multiple choice

Why are fixed-size binary records convenient?

Show answer

Answer: B. Record n starts at byte n × sizeof(record), so it can be reached directly

Fixed sizes make positions predictable, which is the basis of random access with fseek. Binary files are not readable in editors and not always portable.

Question 6 · short answer

Write statements that save an int array marks[50] to marks.dat in one call and check that the whole array was written.

Show worked answer

FILE *fp = fopen("marks.dat", "wb"); if (fp == NULL) { perror("marks.dat"); return 1; } if (fwrite(marks, sizeof marks[0], 50, fp) != 50) printf("Write error\n"); fclose(fp);. sizeof marks[0] is the size of one int, 50 is the number of items, and comparing the return value with 50 confirms that every item reached the file.

↑ ↓ move↵ openTab switch type