I would like to paste my code, if you need it ( c PROGRAMMING, VISUAL STUDIOS) [
ID: 3872004 • Letter: I
Question
I would like to paste my code, if you need it ( c PROGRAMMING, VISUAL STUDIOS)
[THIS IS MY MAIN]
#include "header.h"
void main(void)
{
link ptr, firstptr, lastptr;
char sel;
while (true)
{
printf(" Select Option: ");
printf("(1) Enter new database ");
printf("(2) Read database fromn disk ");
printf("(3) Save current database ");
printf("(4) Display current database in memory ");
printf("(5) Exit the database ");
sel = _getche();
switch (sel)
{
case '1':
firstptr = NULL;
lastptr = NULL;
{
ptr = liquor();
if (!firstptr)
firstptr = ptr;
else {
lastptr->next = ptr;
ptr->last = lastptr;
lastptr = ptr;
break;
}
printf("Do you want to choose another option ?");
sel = _getche();
if (sel == 'n' || sel == 'N')
break;
}
case '2':
firstptr = readdb();
if (firstptr == NULL)
{
printf(" The specified file does not exist. Try again ");
break;
}
break;
case '3':
writedb(firstptr);
break;
case '4':
dispdb(firstptr);
break;
case '5':
exit(0);
default:
printf("This was an invalid option.TRY AGAIN");
}
}
}
[THIS IS MY WRITE SOURCE]
#include "header.h"
int writedb(link ptr)
{
FILE *fptr; //creates file structure
char fname[20]; //place for the name of your disk file
printf("Enter name for database file: ");
gets_s(fname); //enter name you want the file to be called
if ((fptr = fopen(fname, "wb")) == NULL)
{
printf(" File fail on open");
return 1;
}
do
{
fwrite(ptr, sizeof(struct db), 1, fptr);
ptr = ptr->next;
} while (ptr != NULL);
fclose(fptr);
return 0;
}
[THIS IS MY READ SOURCE]
#include "header.h"
link readdb(void)
{
link firstptr, lastptr, ptr; //declare the pointers
FILE *fptr; //open file structure
char fname[20]; //string for file name
size_t read_flag; //return value for file readoperation
firstptr = NULL;
lastptr = NULL;
printf("Enter name for database file: ");
gets_s(fname);
if ((fptr = fopen(fname, "rb")) == NULL)
{
printf(" File fail on open");
return NULL;
}
while (true)
{
ptr = (link)malloc(sizeof(struct db));
if ((read_flag = fread(ptr, sizeof(struct db), 1, fptr)) == 0)
{
free(ptr);
fclose(fptr);
return firstptr;
}
else
{
if (!firstptr)firstptr = ptr;
else lastptr->next = ptr;
ptr->last = lastptr;
lastptr = ptr;
}
}
}
[THIS IS MY DISPLAY SOURCE]
#include "header.h"
void dispdb(link ptr)
{
printf("Quantity Description Price Discount Amount "); //The header for the display goes here
do
{
//Print each item of the structure here
printf("%5s %20s %10s %10s %15s ", ptr->Quantity, ptr->Description, ptr->Price, ptr->Discount, ptr->Amount);
ptr = ptr->next; //point to the next structure in the list
} while (ptr != NULL); //if we get to NULL, the list is printed
//the do loop
printf(" "); //print a couple of blank lines before returning
return;
}
[THIS IS MY HEADER]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
typedef struct db * link;
struct db
{
char Quantity[4];
char Description[30];
char Price[4];
char Discount[4];
char Amount[4];
link next;
link last;
};
link liquor();
void dispdb(link);
int writedb(link);
link readdb();
link liquor();
[THIS IS MY ADDING TO A STRUCTURE SOURCE]
#include "header.h"
link liquor(void)
{
link ptr; //create a pointer to structure
ptr = (link)malloc(sizeof(struct db));
printf("Enter Quantity: ");
gets_s(ptr->Quantity);
printf("Enter Description: ");
gets_s(ptr->Description);
printf("Enter Price: ");
gets_s(ptr->Price);
printf("Enter Discount: ");
gets_s(ptr->Discount);
printf("Enter Amount: ");
gets_s(ptr->Amount);
ptr->next = NULL;
ptr->last = NULL;
return ptr;
}
elect Option: (1) Enter new database (2) Read database fromn disk 3) Save current database (a) Display current database in memory Exit the database Enter Quantity: 4 nter Description: 4 nter Price: 4 nter Discount: 4 nter Anount: 4 Do you want to choose another option ?m elect Option: 1) Enter new database (2) Read database fromn disk (3) Save current database 4) Display current database in memory 5) Exit the database Enter name for database file: qq elect Option: (1) Enter new database (2) Read database fromn disk (3) Save current database (4) Display current database in memory (5) Exit the database Enter Quantity: s Enter Description: 5 nter Price: 5 nter Discount: nter Amount 5 Do you want to choose another option ?n elect Option: 1) Enter new database 2) Read database fromn disk 3) Save current database 4) Display current database in memory 5) Exit the database Enter name for database file: q.q Select Option: 1) Enter new database (2) Read database fromn disk 3) Save current database (4) Display current database in memory (5) Exit the database Enter name for database file: q.q Select Option: 1) Enter new database 2) Read database fromn disk 3) Save current database (4) Display current database in memory 5) Exit the database Quantity Description Price Discount Amount 5Explanation / Answer
You should open the file in append mode instead of write mode, because whenever you will open a file in the write mode it will delete all the previous content of the file and will overridde the previous content with the new content on the file. But the append mode will add the new data to the previously written data to the file.