Computer ScienceNEB 2081
Write a C program that uses structures to represent details of five books (title, author, publisher and price) and prints them out. [8] OR Discuss the concept of binary file handling in C…
8Write a C program that uses structures to represent details of five books (title, author, publisher and price) and prints them out. [8] OR Discuss the concept of binary file handling in C programming and explain how putw( ) and getw( ) functions facilitate binary input/output operations. Give examples. [8]
Answer
Model Answer for NEB 2081 (Class 12, Computer Science)
Part 1: C Program to Store and Print Book Details Using Structures
#include <stdio.h>
#include <string.h>
```figure
{"type":"array","values":[{"label":"Book 1","fields":{"title":"The C Programming Language","author":"K&R","publisher":"Prentice Hall","price":1200}},{"label":"Book 2","fields":{"title":"Data Structures","author":"Horstmann","publisher":"Pearson","price":1500}},{"label":"Book 3","fields":{"title":"Operating Systems","author":"Silberschatz","publisher":"Wiley","price":1800}},{"label":"Book 4","fields":{"title":"Computer Networks","author":"Forouzan","publisher":"McGraw-Hill","price":1600}},{"label":"Book 5","fields":{"title":"Algorithms","author":"Cormen","publisher":"MIT Press","price":2000}}],"caption":"Structure array representation of 5 books (as used in the program)"}
// Define a structure to store book details struct Book { char title[50]; char author[50]; char publisher[50]; float price; };
int main() { // Declare an array of 5 Book structures struct Book books[5];
// Input details for 5 books
for (int i = 0; i < 5; i++) {
printf("\nEnter details for Book %d:\n", i + 1);
printf("Title: ");
fgets(books[i].title, 50, stdin);
books[i].title[strcspn(books[i].title, "\n")] = '\0'; // Remove newline
printf("Author: ");
fgets(books[i].author, 50, stdin);
books[i].author[strcspn(books[i].author, "\n")] = '\0';
printf("Publisher: ");
fgets(books[i].publisher, 50, stdin);
books[i].publisher[strcspn(books[i].publisher, "\n")] = '\0';
printf("Price: ");
scanf("%f", &books[i].price);
while (getchar() != '\n'); // Clear input buffer
}
// Print the details of all books
printf("\n--- Book Details ---\n");
for (int i = 0; i < 5; i++) {
printf("\nBook %d:\n", i + 1);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Publisher: %s\n", books[i].publisher);
printf("Price: %.2f\n", books[i].price);
}
return 0;
}
How it works:
- A
struct Bookis defined to store title, author, publisher, and price of a book. - An array of 5 such structures (
books[5]) is declared to store details of 5 books. - A loop takes input for each book using
fgets()(to handle spaces in names) andscanf()(for price). - Another loop prints the stored details in a structured format.
Part 2: Binary File Handling in C (Alternative Answer)
Concept of Binary File Handling
Binary file handling in C involves reading/writing data in its raw binary format (not text) directly to/from a file. Unlike text files (where data is converted to ASCII/Unicode), binary files store data exactly as it is in memory, making them faster and more efficient for large datasets (e.g., images, databases, executable files).
Key advantages:
- Speed: No conversion overhead (faster I/O).
- Memory efficiency: Stores data compactly (e.g., integers as 4 bytes, not as text).
- Exact representation: Preserves data types (e.g.,
float,struct) without corruption.
Role of putw() and getw()
These functions are used for binary I/O of integers (specifically int type) in C.
| Function | Description | Example |
|---|---|---|
putw(int value, FILE *stream) |
Writes an integer (int) in binary to a file. |
putw(100, fp); writes 100 as binary to fp. |
getw(FILE *stream) |
Reads an integer (int) from a binary file. |
num = getw(fp); reads an int from fp. |
Limitations:
- Only works with
int(notfloat,char, or structures). - Deprecated in modern C (replaced by
fwrite()/fread()for portability).
Example: Storing and Retrieving Integers in Binary Mode
#include <stdio.h>
int main() {
FILE *fp;
int num, i;
// Write integers to a binary file
fp = fopen("numbers.bin", "wb"); // "wb" = write binary
for (i = 1; i <= 5; i++) {
num = i * 10;
putw(num, fp); // Store each number in binary
}
fclose(fp);
// Read integers from the binary file
fp = fopen("numbers.bin", "rb"); // "rb" = read binary
printf("Numbers read from binary file:\n");
for (i = 0; i < 5; i++) {
num = getw(fp); // Retrieve each number
printf("%d ", num);
}
fclose(fp);
return 0;
}
Output:
Numbers read from binary file:
10 20 30 40 50
Explanation:
- Writing:
- Opens
"numbers.bin"in binary write mode ("wb"). - Uses
putw()to store integers10, 20, ..., 50directly in binary.
- Opens
- Reading:
- Opens the same file in binary read mode (
"rb"). - Uses
getw()to retrieve integers in the same order.
- Opens the same file in binary read mode (
Modern Alternative: fwrite() and fread()
For structures or non-integer data, use:
struct Book {
char title[50];
float price;
};
// Writing a structure to binary file
fwrite(&book, sizeof(struct Book), 1, fp);
// Reading a structure from binary file
fread(&book, sizeof(struct Book), 1, fp);
Why prefer fwrite()/fread()?
- Works with any data type (structures, arrays, floats).
- More portable across systems.
- Supports multiple records (e.g.,
fwrite(&books, sizeof(books), 5, fp)for 5 books).
Discussion
Loading…