Im still unable to make this program work...
Ok, here's what I did:
In the menu I gave the user a choice to add new product, modify the name of a specific product or exit. In the structure i removed all numeral values and left just 3 chars set to 5 bytes each so that it would be easier to read it from the file...
I added 3 new items to the "database":
Value1: Value2: Value3:
11111 aaaaa zzzzz
22222 bbbbb yyyyy
33333 cccccc xxxxx
So, what i get written into the file are 3 separate entries, all follow each other:
Code:
11111aaaaazzzzz22222bbbbbyyyyy33333cccccxxxxx
Now i run the program and go to part b (to modify the name - 11111, 22222 or 33333)...
The program asks me what product i want to change (1-3). I say 2. Write the new description: 55555 and get the following in the updated file:
Code:
11111aaaaazzzzz55555 A 33333cccccxxxxx
The above happens when I exit the program and reenter to edit the name... If I dont exit, and modify the description as soon as i add the 3 products i get the following:
Code:
11111aaaaazzzzz55555 ccccxxxxx33333cccccxxxxx
How do I go about making this work?
P.S.
Here's the code:
Code:
#include <stdio.h>
struct product {
char name[5];
char price[5];
char quantity[5];
};
int main() {
int menu_stock(char*);
char choice_stock;
void add_product(product*);
void change_description(product*);
void seek_product_number(int, FILE*);
int total_products(FILE*);
int get_product_number(int);
void seek_end_db(FILE*);
void write_to_db(FILE*, product*);
product p;
FILE *file_sales;
file_sales = fopen("./sales.txt", "r+");
while(menu_stock(&choice_stock) == 1) {
switch(choice_stock) {
case 'A':
case 'a':
add_product(&p);
seek_end_db(file_sales);
write_to_db(file_sales, &p);
break;
case 'B':
case 'b':
seek_product_number(get_product_number(total_products(file_sales)), file_sales);
change_description(&p);
write_to_db(file_sales, &p);
break;
}
}
}
int menu_stock(char *c) {
void clear_input_buffer();
do {
printf("*****************************************\n");
printf("* Add new product (A) *\n");
printf("* Change product description (B) *\n");
printf("* Exit (X) *\n");
printf("*****************************************\n");
printf(" --> ");
*c = (char)getchar();
clear_input_buffer();
} while(*c != 'A' && *c != 'a' && *c != 'B' && *c != 'b' && *c != 'X' && *c != 'x');
if(*c == 'X' || *c == 'x') return 0;
else return 1;
}
void clear_input_buffer() {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
clearerr(stdin);
}
void add_product(product *p) {
printf("Enter product description : ");
gets(p->name);
printf("Enter price per unit (cent) : ");
gets(p->price);
printf("Enter stock level: ");
gets(p->quantity);
}
void change_description(product *p) {
printf("Enter product description : ");
gets(p->name);
}
void seek_end_db(FILE *file_sales) {
fseek(file_sales, 0, SEEK_END);
}
void write_to_db(FILE *file_sales, product *p) {
fwrite(p, sizeof(product), 1, file_sales);
}
void seek_product_number(int product_number, FILE *file_sales) {
fseek(file_sales, (product_number - 1) * sizeof(product), SEEK_SET);
}
int total_products(FILE *file_sales) {
int file_size;
fseek(file_sales, 0, SEEK_END);
file_size = ftell(file_sales);
return file_size / sizeof(product);
}
int get_product_number(int totalproducts) {
int product_number;
void clear_input_buffer();
if(totalproducts <= 1) {
return 1;
}
else {
do {
printf("Enter product number (1 - %d): ", totalproducts);
scanf("%d", &product_number);
clear_input_buffer();
} while(product_number <= 0 || product_number > totalproducts);
return product_number;
}
}