I'm a bit new when it comes to files and records in C
, and i'm currently working on a project. The following program, must give the user the options to edit,add,delete and view all records on a file.
#include <stdio.h>
typedef struct {
int code;
char description[100];
int volume;
} product;
void addition(FILE *filePtr);
void removal(FILE *filePtr);
void showcase(FILE *filePtr);
void edit(FILE *filePtr);
void description(FILE *filePtr);
int main() {
int pick, i;
product p = { 0, "", 0 };
FILE *filePtr;
if ( ( filePtr = fopen( "stock.dat", "a+") ) == NULL ) {
printf("The file could not be read");
} else {
for( i = 0; i < 100; i++ )
fwrite( &p, sizeof(p) , 1 , filePtr) ;
fclose(filePtr) ;
while ( !feof( filePtr)) {
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 change a product's description*\n");
printf("********************************************\n");
scanf("%d", &pick);
switch ( pick ) {
case 1:
removal(filePtr);
break;
case 2:
edit(filePtr);
break;
case 3:
addition(filePtr);
break;
case 4:
showcase(filePtr);
break;
case 5:
description(filePtr);
break;
default:
printf("Error! Please enter a correct number");
break;
}
}
}
return 0;
}
void removal(FILE *cfPtr) {
product p;
int code, volume;
product empty = { 0, "", 0 };
cfPtr = fopen("stock.dat", "a");
printf("\nYou are about to remove a product from the list\n");
printf("Please type that product's code\n");
scanf("%d", &code);
fseek(cfPtr, (p.code - 1)*sizeof(p), SEEK_SET);
fread(&p, sizeof(p), 1, cfPtr);
if (code == 0) {
printf("\nProduct code %d not found, please try again\n", &code);
} else {
printf("\nProduct found! Record is being deleted...");
fseek(cfPtr, (p.code - 1)*sizeof(p), SEEK_SET);
fwrite(&empty, sizeof(p), 1, cfPtr);
printf("\nRecord succesfully deleted!\n");
}
fclose(cfPtr);
}
void addition(FILE *cfPtr) {
product p;
int code, quantity;
printf("You are about to add a new product in the list\n");
cfPtr = fopen("stock.dat", "a");
printf("Please input the product's code that you wish to add\n");
fscanf( stdin, "%d", &p.code);
printf("Now input the available quantity of the product that is in stock\n");
fscanf(stdin, "%d", &p.volume);
fseek( cfPtr, (p.code - 1) * sizeof(p), SEEK_SET);
fseek( cfPtr, (p.volume - 1) * sizeof(p), SEEK_SET);
fwrite(&code, sizeof(p), 1, cfPtr);
fwrite(&quantity, sizeof(p), 1, cfPtr);
printf("\n\nThe Product with code %d and quantity of %d has been added succesfully\n\n", p.code, p.volume);
fclose(cfPtr);
}
void showcase(FILE *cfPtr){
product p;
int i;
cfPtr = fopen("stock.dat", "a");
printf("\n\n\n ****SHOWING ALL PRODUCTS****\n\n\n");
printf("PRODUCT CODE QUANTITY IN STOCK");
for (i=0; i=100; i++) {
printf("\n%d", p.code);
printf(" %d\n", &p.volume);
}
fclose(cfPtr);
}
void edit(FILE *cfPtr){
product p;
int code, volume;
cfPtr= fopen("stock.dat", "a");
printf("\n\n\n\You are about to edit a product's quantity\n");
printf("Please enter that product's code\n");
scanf("%d", &code);
while ( !feof( cfPtr)) {
fseek(cfPtr, ( code - 1)*sizeof(p), SEEK_SET);
fread( &code, sizeof(p), 1, cfPtr);
if (code = p.code){
printf("Product %d was found! current quantity: %d\n", &code, &p.volume);
printf("Please enter the new quantity of the product\n");
scanf("%d", &volume);
fseek(cfPtr, ( volume - 1)*sizeof(p), SEEK_SET);
fwrite( &p, sizeof(p), 1, cfPtr);
printf("List has been updated, product %d has now %d units\n\n", &code, &volume);
break;
} else {
printf("Product code %d was not found please try again", &code);
}
}
fclose(cfPtr);
}
void description(FILE *cfPtr) {
product p;
char desc;
int i, code, check=0;
cfPtr= fopen("stock.dat", "a");
printf("\n You are about to change a product's description\n");
printf("Please enter the product's code");
scanf("%d", &code);
for (i=0; i=100; i++) {
fseek( cfPtr, (code -1)*sizeof(p), SEEK_SET);
fread( &code, sizeof(p), 1, cfPtr);
if (code = p.code) {
printf("\nCode found, now please enter the description");
scanf("%c", &desc);
p.description[i] = 'desc';
fwrite( &p, sizeof(p), 1, cfPtr);
check = 1;
}
}
if (check != 1){
printf("Code not found! Please try again");
}
}
This is not the final code, and it surely has a lot of mistakes in it, what concerns me however most of all are the following code lines:
void showcase(FILE *cfPtr) {
product p;
int i;
cfPtr = fopen("stock.dat", "a");
printf("\n\n\n ****SHOWING ALL PRODUCTS****\n\n\n");
printf("PRODUCT CODE QUANTITY IN STOCK");
for (i=0; i<100; i++) {
printf("\n%d", p.code);
printf(" %d\n", &p.volume);
}
fclose(cfPtr);
}
When this section runs, the program goes on and on nonstop, showing nonsense records. I've been working on this for 2 days, I've researched all I could, but nevertheless didn't find a solution myself. Again, I'm very new at this, and surely my stupid mistakes are probably going to get a lot of hate.Anyhow i would greatly appreciate even a hint of what I've done wrong, to make the program not properly save the record and show them.
Thanks in advance :)
UPDATE minor mistake was fixed on the aforementioned function, but the code still fails to save newly written records..