Code:
#include <stdio.h>
typedef struct{
int code;
char description[100];
int volume;
}product;
int main() {
int pick =0, i;
product *p;
FILE *filePtr;
void add(FILE *filePtr, product* p);
void removal(FILE *filePtr, product* p);
void show(FILE *filePtr, product* p);
void edit(FILE *filePtr, product* p);
void description(FILE *filePtr, product* p);
if ( ( filePtr = fopen( "stock.dat", "ab+")) == NULL ) {
printf("The file could not be read");
}
else {
do{
printf(" ************ MAIN MENU ************\n");
printf(" ** -------------welcome------------- **\n");
printf(" ** ** \n");
printf("* Please select one of the commands below *\n");
printf("********************************************\n");
printf("* Press 1 to remove products from the list *\n");
printf("* Press 2 to change a product's quantity *\n");
printf("* Press 3 to add a new product to the list *\n");
printf("* Press 4 to view product list *\n");
printf("* Press 5 to exit the programm *\n");
printf("********************************************\n");
scanf("%d", &pick);
switch ( pick ) {
case 1:
removal(filePtr, p);
break;
case 2:
edit(filePtr, p);
break;
case 3:
add(filePtr, p);
break;
case 4:
show(filePtr, p);
break;
default:
printf("Error! Please enter a correct number");
break;
}
}while (pick != 5);
}
fclose(filePtr);
return 0;
}
void removal(FILE *fileptr, product * p) {
int code;
product empty = { 0, " ", 0 };
printf("\n\nYou are about to delete a record, please enter the product's code");
scanf("%d", &code);
fseek( fileptr, (code - 1) * sizeof(p), SEEK_SET);
fread( &p->code, sizeof(p), 1, fileptr);
if (p->code == 0){
printf("Product code not found, please retry");
} else {
fseek( fileptr, (code -1) * sizeof(p), SEEK_SET);
fwrite( &empty, sizeof(p), 1, fileptr);
printf("Code was found, product removed successfully\n\n");
}
}
void add(FILE *fileptr, product * p){
int code, quantity;
printf("You are about to add a new product in the list\n Please input the product's code\n");
scanf("%d", &code);
fseek( fileptr, (code-1) * sizeof(p), SEEK_SET);
fread(&code, sizeof(p), 1, fileptr);
fseek(fileptr, (code-1) * sizeof(p), SEEK_SET);
fwrite( &code, sizeof(p), 1, fileptr);
printf("Now enter the product's remaining quantity");
scanf("%d", &quantity);
fseek(fileptr, (quantity-1) * sizeof(p), SEEK_SET);
fwrite( &quantity, sizeof(p), 1, fileptr);
}
void edit(FILE *fileptr, product * p){
int code, volume;
printf("\nYou are about to edit a existing product's quantity\n Please enter the product's code\n");
scanf("%d", &code);
fseek(fileptr, (code -1) * sizeof(p), SEEK_SET);
fread(&code, sizeof(p), 1, fileptr);
if (p->code == 0){
printf("\nThe code you entered is not in the list, please retry\n");
}else{
printf("\nCode found!\n Product's current quantity is %d", &p->volume);
printf("\n Please enter the new quantity");
scanf("%d", &volume);
p->volume = volume;
fseek(fileptr, (volume -1) * sizeof(p), SEEK_SET);
fwrite(&volume, sizeof(p), 1, fileptr);
}
}
void show(FILE *fileptr, product * p){
int i=1;
printf("\n\n\n ****SHOWING ALL PRODUCTS****\n\n\n");
printf("PRODUCT CODE QUANTITY IN STOCK\n");
while (i<100 && !feof(fileptr)){
fscanf( fileptr, "%d%d", &p->code, &p->volume);
printf("%d %d\n", &p->code, &p->volume);
fseek(fileptr, (p->code + 1) * sizeof(p), SEEK_SET);
fscanf( fileptr, "%d%d", &p->code, &p->volume);
i++;
}
printf("You may scroll back up to view the list\n\n");
}
Any ideas why :/ ?