Professional Documents
Culture Documents
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';
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);
printf("\n\n");
printf("Update Another Record (Y/N): ");
fflush(stdin);
another = toupper(getchar());
}
break;
case '4':
another = 'Y';
ft = fopen("TEMP.DAT", "wb");
rewind(fp);
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;
}