#include <stdio.
h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "products.bin"
typedef struct {
int product_id;
char product_name[50];
float price;
int quantity;
} Product;
void writeProducts();
void appendProducts();
void readProducts();
void modifyProduct();
void displayMenu();
int main() {
int choice;
do {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
writeProducts();
break;
case 2:
appendProducts();
break;
case 3:
readProducts();
break;
case 4:
modifyProduct();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
void displayMenu() {
printf("\n-- Product Management System --\n");
printf("1. Write Products\n");
printf("2. Append Products\n");
printf("3. Read Products\n");
printf("4. Modify Product\n");
printf("5. Exit\n");
}
void writeProducts() {
FILE *file = fopen(FILE_NAME, "wb");
if (!file) {
printf("Error opening file.\n");
return;
}
int count;
printf("Enter the number of products to write: ");
scanf("%d", &count);
getchar();
Product p;
for (int i = 0; i < count; i++) {
printf("\nEnter details for product %d:\n", i + 1);
printf("Product ID: ");
scanf("%d", &p.product_id);
getchar();
printf("Product Name: ");
fgets(p.product_name, 50, stdin);
p.product_name[strcspn(p.product_name, "\n")] = 0;
printf("Price: ");
scanf("%f", &p.price);
printf("Quantity: ");
scanf("%d", &p.quantity);
fwrite(&p, sizeof(Product), 1, file);
}
fclose(file);
printf("Products have been written to the file successfully.\n");
}
void appendProducts() {
FILE *file = fopen(FILE_NAME, "ab");
if (!file) {
printf("Error opening file.\n");
return;
}
int count;
printf("Enter the number of products to append: ");
scanf("%d", &count);
getchar();
Product p;
for (int i = 0; i < count; i++) {
printf("\nEnter details for product %d:\n", i + 1);
printf("Product ID: ");
scanf("%d", &p.product_id);
getchar();
printf("Product Name: ");
fgets(p.product_name, 50, stdin);
p.product_name[strcspn(p.product_name, "\n")] = 0;
printf("Price: ");
scanf("%f", &p.price);
printf("Quantity: ");
scanf("%d", &p.quantity);
fwrite(&p, sizeof(Product), 1, file);
}
fclose(file);
printf("Products have been appended to the file successfully.\n");
}
void readProducts() {
FILE *file = fopen(FILE_NAME, "rb");
if (!file) {
printf("Error opening file.\n");
return;
}
printf("\nReading products from the file:\n");
printf("-----------------------------------------------------\n");
printf("Product ID Product Name Price Quantity\n");
printf("-----------------------------------------------------\n");
Product p;
while (fread(&p, sizeof(Product), 1, file)) {
printf("%-15d %-20s %-8.2f %-10d\n", p.product_id, p.product_name, p.price, p.quantity);
}
printf("-----------------------------------------------------\n");
fclose(file);
}
void modifyProduct() {
FILE *file = fopen(FILE_NAME, "rb+");
if (!file) {
printf("Error opening file.\n");
return;
}
int id, found = 0;
printf("Enter the Product ID to modify: ");
scanf("%d", &id);
getchar();
Product p;
while (fread(&p, sizeof(Product), 1, file)) {
if (p.product_id == id) {
found = 1;
printf("Product found. Enter new details:\n");
printf("New Product Name: ");
fgets(p.product_name, 50, stdin);
p.product_name[strcspn(p.product_name, "\n")] = 0;
printf("New Price: ");
scanf("%f", &p.price);
printf("New Quantity: ");
scanf("%d", &p.quantity);
fseek(file, -sizeof(Product), SEEK_CUR);
fwrite(&p, sizeof(Product), 1, file);
printf("Product updated successfully.\n");
break;
}
}
if (!found) {
printf("Product not found.\n");
}
fclose(file);
}