Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct inventory {
char sku[10];
char description[30];
double price;
int quantity;
} inv_list[MAX];
void init_list(void),
enter(void),
create(void),
list(void);
int menu_select(void),
update(void),
delete(void),
load(void),
find_free(void),
save(void);
int main(void)
{
char choice;
init_list(); /* initialize the structure array */
for (;;)
{
choice = menu_select();
switch(choice)
{
case 1:
enter();
break;
case 2:
delete();
break;
case 3:
update();
break;
case 4:
list();
break;
case 5:
save();
break;
case 6:
load();
break;
case 7:
exit(0);
break;
default:
printf("Invalid menu item selected. Please try again\n\n");
break;
}
}
return 0;
}
/* Initialize the list. */
void init_list(void)
{
register int t;
for (t = 0; t < MAX; ++t)
inv_list[t].sku[0] = '\0';
}
/* Get a menu selection. */
int menu_select(void)
{
char s[80];
int c;
printf("1. Enter a record \n");
printf("2. Delete a record\n");
printf("3. Update a record\n");
printf("4. List the file\n");
printf("5. Save the file\n");
printf("6. Load the file\n");
printf("7. Quit\n");
do
{
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c < 0 || c > 7);
return c;
}
/* Input invesses into the list. */
void enter(void)
{
// FILE *fPtr;
int slot;
char s[80];
// char answer;
// char output[80];
slot = find_free();
if (slot == -1)
{
printf("\nList Full");
return;
}
printf("Enter sku: ");
gets(inv_list[slot].sku);
printf("Enter description: ");
gets(inv_list[slot].description);
printf("Enter price: ");
gets(s);
inv_list[slot].price = atof(s);
printf("Enter quantity: ");
gets(s);
inv_list[slot].quantity = atoi(s);
}
/* Find an unused structure. */
int find_free(void)
{
register int t;
for (t = 0; inv_list[t].sku[0] && t < MAX; ++t) ;
if (t == MAX)
return -1; /* no slots free */
return t;
}
/* Delete a record. */
int delete (void)
{
struct inventory delrec = {"","",0.00,0};
FILE *fPtr;
// register int inv;
// char invty[10];
char output[80];
char answer;
int i = 0;
int rec;
printf("Enter file name you want to delete a record from ");
gets(output);
if ((fPtr = fopen(output, "r+")) == NULL)
{
printf("Cannot open file.\n\n");
return 0;
}
printf( "Enter record # that you want to delete: " );
scanf("%d", &rec);
//gets(invty);
//inv = atoi(invty);
rewind (fPtr);
fseek( fPtr, ((rec-1)*62), SEEK_SET );
fread(&inv_list[i], sizeof(struct inventory), 1, fPtr);
printf("Are you sure you want to delete information to record # %d", rec);
printf("Enter y for yes");
fflush(stdin);
scanf("%c", &answer);
if (answer = 'Y')
{
rewind(fPtr);
fseek(fPtr, (rec-1)*sizeof(struct inventory), SEEK_SET);
fwrite(&delrec, sizeof(struct inventory), 1, fPtr);
printf("You record has been deleted");
}
fclose(fPtr);
// }
return 0;
}
/* Display the list on the screen. */
void list(void)
{
register int t;
for (t = 0; t < MAX; ++t)
{
if(inv_list[t].sku[0])
{
printf("%s\n", inv_list[t].sku);
printf("%s\n", inv_list[t].description);
printf("%f\n", inv_list[t].price);
printf("%d\n\n", inv_list[t].quantity);
}
}
printf("\n\n");
}
/* Save the list. */
int save(void)
{
FILE *fp;
char output[80];
char reply[20];
register int i;
printf("Enter file you want to save to. ");
gets(output);
if ((fp = fopen(output, "w")) == NULL)
{
printf("Cannot open file.\n\n");
exit(1);
}
if (fp != NULL)
{
printf("File aready exists. Do you want to overwrite Y or N: ");
gets(reply);
if (reply[0] == 'N' || reply[0] == 'n')
{
printf("Do you want to create a new file? ");
gets(reply);
if (reply[0] = 'Y' || reply[0] == 'y')
{
create();
}
}
else
{
}
for (i=0; i<MAX; i++)
{
if(inv_list[i].sku[0])
{
if ((fprintf(fp, "%-09s", inv_list[i].sku))> 10)
{
fprintf(stderr, " Unable to save to a file. The sku is too long \n\n");
fclose(fp);
return 0;
}
if ((fprintf(fp, "%-29s", inv_list[i].description)) > 30)
{
fprintf(stderr, " Unable to save to a file. The description is too long\n\n");
}
if ((fprintf(fp, "%.02f", inv_list[i].price)) > 10.2)
{
fprintf(stderr, " Unable to save to a file. The price is too long\n\n");
fclose(fp);
return 0;
}
if ((fprintf(fp, "%08d\n", inv_list[i].quantity)) > 9)
{
fprintf(stderr, " Unable to save to a file. The quantity is too long\n\n");
fclose(fp);
return 0;
}
}
}
printf("File %s saved\n\n", output);
}
fclose(fp);
return 0;
}
/* Load the file. */
int load(void)
{
FILE *fPtr;
char output[80];
char price[20];
char quant[20];
register int i;
printf("Enter the file name you want to open ");
gets(output);
if ((fPtr = fopen(output, "r")) == NULL)
{
printf("Cannot open file.\n\n");
exit(1);
}
init_list();
for(i=0;i<MAX; i++)
{
if(! fgets(inv_list[i].sku, 10, fPtr))
{
if (feof(fPtr))
{
break;
}
fprintf(stderr, " Unable to read File");
fclose(fPtr);
return 0;
}
if (! fgets(inv_list[i].description, 30, fPtr))
{
if (feof(fPtr))
{
break;
}
fprintf(stderr, " Unable to read File");
fclose(fPtr);
return 0;
}
if (! fgets(price, 12, fPtr))
{
if (feof(fPtr))
{
break;
}
fprintf(stderr, " Unable to read from the file");
fclose(fPtr);
return 0;
}
inv_list[i].price = atof(price);
if (! fgets(quant, 8, fPtr))
{
if (feof(fPtr))
{
break;
}
fprintf(stderr, " Unable to read from the file");
fclose(fPtr);
return 0;
}
inv_list[i].quantity = atoi(quant);
}
/*for (i = 0; i < MAX; i++)
if (fread(&inv_list[i], sizeof(struct inventory), 1, fp) != 1)
{
if (feof(fp))
break;
printf("File read error.\n");
}*/
printf("File %s loaded and ready to be read from.\n\n", output);
fclose(fPtr);
return 0;
}
void create(void)
{
FILE *fPtr;
char output[80];
char reply [20];
printf("Enter the name of file you want to be created: ");
gets(output);
printf("\n");
if ((fPtr = fopen(output, "r+")) == NULL)
{
printf("File could not be created");
exit(1);
}
if ((fPtr = fopen(output, "r+"))!= NULL)
{
//fclose(fPtr);
printf("File Exist. Overwrite this file? (Y or N): ");
gets (reply);
if (reply[0] != 'Y' || 'y')
{
printf("%s file Overwritten\n\n", output);
}
else
{
printf("File '%s' Created\n\n", output);
}
}
if ((fPtr = fopen(output, "w")) == NULL)
{
printf("Unable to open file to write");
}
}
int update(void)
{
FILE *fPtr;
register int inv;
char invty[80];
char output[80];
int i = 0;
printf("Enter file name you want to update ");
gets(output);
if ((fPtr = fopen(output, "r+")) == NULL)
{
printf("Cannot open file.\n\n");
exit(1);
}
printf( "Enter record # that you want to update: " );
gets(invty);
inv = atoi(invty);
rewind (fPtr);
fseek( fPtr, ((inv-1)*62), SEEK_SET );
fread(&inv_list[i], sizeof(struct inventory), 1, fPtr);
printf("Enter new sku #: ");
gets(inv_list[i].sku);
printf("Enter new description: ");
gets(inv_list[i].description);
printf("Enter new price: ");
scanf("%10.2f\n", &inv_list[i].price);
printf("Enter new quantity: ");
scanf("%8\n", &inv_list[i].quantity);
fseek(fPtr, ((inv-1)*62), SEEK_SET);
fwrite( &inv_list[i], sizeof( struct inventory ), 1, fPtr );
fclose(fPtr);
return 0;
}
Thanks in advance