0% found this document useful (0 votes)
58 views4 pages

Car Brand

This C program allows users to manage a database of car brands and prices stored in a binary file. The program displays a menu with options to add, view, update, and delete car records from the file. It uses structures to store the brand and price as fields, and functions like fread, fwrite to read from and write new records to the file. Records can be searched, modified and deleted by comparing the brand fields in the file.

Uploaded by

romeo Solis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Car Brand

This C program allows users to manage a database of car brands and prices stored in a binary file. The program displays a menu with options to add, view, update, and delete car records from the file. It uses structures to store the brand and price as fields, and functions like fread, fwrite to read from and write new records to the file. Records can be searched, modified and deleted by comparing the brand fields in the file.

Uploaded by

romeo Solis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

struct car{
char brand[100];
char price[100];
};

int main() {
FILE *fp, *ft;
char another, choice;
struct car info;
char car_title[100];
long int recsize;

fp = fopen("car.txt", "rb+");

if (fp == NULL) {
fp = fopen("car.txt", "wb+");

if (fp == NULL) {
puts("Cannot open file");
exit(0);
}
}

recsize = sizeof(info);

while (1) {
system("CLS");
printf("\n");
printf("\n\t========================================================");
printf("\n\t\t\t\tCAR BRANDS");
printf("\n\t========================================================");

printf("\n\n");
printf("\t1. ADD CARS");
printf("\n");
printf("\t2. DISPLAY CAR BRANDS AND PRICES");
printf("\n");
printf("\t3. UPDATE CAR RECORDS");
printf("\n");
printf("\t4. DELETE CAR RECORDS");
printf("\n");
printf("\t5. QUIT PROGRAM");
printf("\n");
printf("\tSELECT YOUR CHOICE:");
fflush(stdin);
choice = getchar();

switch (choice) {
case '1':
fseek(fp, 0, SEEK_END);
another = 'Y';

while (another == 'Y') {


system("cls");
printf("\n\n");
printf("====Add Car Brands====");
printf("\n\n");
printf("Enter Car Brand : ");
scanf("%s", info.brand);
printf("Enter Car Price : ");
scanf("%s", info.price);
fwrite(&info, recsize, 1, fp);
printf("\n\n");
printf("Add another Record (Y/N): ");
fflush(stdin);
another = toupper(getchar());
}
break;

case '2':
system("cls");
rewind(fp);
printf("\n\n");
printf("=== View the Car Records ===");
printf("\n\n");
while (fread(&info, recsize, 1, fp) == 1) {
printf("\n");
printf("Brand : %s", info.brand);
printf("\nPrice : %s", info.price);
printf("\n");
}
printf("\n\n");
system("pause");
break;

case '3':
another = 'Y';
while (another == 'Y') {
system("cls");
printf("==== Update Car Record ===");
printf("\n\n");
printf("Enter Car Brand to Update: ");
scanf("%s", car_title);
rewind(fp);

while (fread(&info, recsize, 1, fp) == 1) {


if (strcmp(info.brand, car_title) == 0) {
printf("Enter Brand : ");
scanf("%s", info.brand);
printf("Enter Your Price : ");
scanf("%s", info.price);
fseek(fp, -recsize, SEEK_CUR);
fwrite(&info, recsize, 1, fp);
break;
}
}

printf("\n\n");
printf("Update Another Record (Y/N): ");
fflush(stdin);
another = toupper(getchar());
}
break;

case '4':
another = 'Y';

while (another == 'Y') {


system("cls");
printf("=== Delete Record ===");
printf("\n\n");
printf("Enter Car Brand to Delete: ");
scanf("%s", car_title);
printf("\n");

ft = fopen("TEMP.DAT", "wb");
rewind(fp);

while (fread(&info, recsize, 1, fp) == 1) {


if (strcmp(info.brand, car_title) != 0) {
fwrite(&info, recsize, 1, ft);
} else {
printf("Record Successfully Deleted from the Database.");
}
}

fclose(fp);
fclose(ft);
remove("car.txt");
rename("TEMP.DAT", "car.txt");
fp = fopen("car.txt", "rb+");
printf("\n");
printf("Delete Another Record (Y/N): ");
fflush(stdin);
another = toupper(getchar());
}
break;

case '5':
fclose(fp);
printf("\n");
printf("Thank you for using this Program");
printf("\n\n");
system("PAUSE");
exit(0);
}
}

return 0;
}

You might also like