2016-04-14 15:27:49 +00:00
|
|
|
#include <stdio.h>
|
2016-04-14 18:28:43 +00:00
|
|
|
#include <stdlib.h>
|
2016-04-14 15:27:49 +00:00
|
|
|
|
|
|
|
#define ROW_LEN 50
|
2016-04-14 17:29:01 +00:00
|
|
|
#define IMEI_LEN 10
|
2016-04-14 15:27:49 +00:00
|
|
|
|
|
|
|
typedef struct Db_tag {
|
2016-04-14 17:29:01 +00:00
|
|
|
int blacklisted; // 1 yes, 0 no
|
2016-04-14 15:27:49 +00:00
|
|
|
char repair_type[ROW_LEN];
|
2016-04-14 17:29:01 +00:00
|
|
|
char imei[IMEI_LEN];
|
2016-04-14 15:27:49 +00:00
|
|
|
int price;
|
|
|
|
int investment;
|
|
|
|
int profit;
|
|
|
|
} Database;
|
|
|
|
|
2016-04-14 18:28:43 +00:00
|
|
|
void display_entry(Database * entry, unsigned index);
|
|
|
|
void insert_entry(FILE * file, Database * entry);
|
|
|
|
void read_entry(FILE * file, Database * entry);
|
2016-04-14 17:29:01 +00:00
|
|
|
|
2016-04-14 18:28:43 +00:00
|
|
|
void delete_entry(FILE * file, unsigned entry_no) {
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
Database entry;
|
|
|
|
short found = 0;
|
|
|
|
FILE * out = fopen(".temp", "wb");
|
|
|
|
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
|
|
|
|
if ( i == entry_no ) {
|
|
|
|
printf("Entry found and deleted!\n");
|
|
|
|
found = 1;
|
|
|
|
} else {
|
|
|
|
insert_entry(out, &entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insert_entry(out, &entry);
|
|
|
|
if ( !found ) {
|
|
|
|
printf("Entry not found!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
system("rm -f data.bdb; mv .temp data.bdb"); // copy and remove, ty unix <3
|
|
|
|
if ( fclose(file) ) {
|
|
|
|
perror("Can't close file!");
|
|
|
|
}
|
|
|
|
if ( !(file = fopen("data.bdb", "r+b")) ) {
|
|
|
|
perror("Can't open file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:34:42 +00:00
|
|
|
void modify_entry(Database * entry) {
|
2016-04-14 18:51:45 +00:00
|
|
|
printf("Enter new repair type:\n");
|
|
|
|
printf("Current: %s\n> ", entry->repair_type);
|
|
|
|
scanf("%50[a-zA-Z ]", entry->repair_type);
|
|
|
|
printf("Enter new price:\n");
|
|
|
|
printf("Current: %d\n> ", entry->price);
|
|
|
|
scanf("%d", &entry->price);
|
|
|
|
printf("Enter new investment:\n");
|
|
|
|
printf("Current: %d\n> ", entry->investment);
|
|
|
|
scanf("%d", &entry->investment);
|
|
|
|
printf("Enter new profit:\n");
|
|
|
|
printf("Current: %d\n> ", entry->profit);
|
|
|
|
scanf("%d", &entry->profit);
|
2016-04-14 18:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_entry(FILE * file, unsigned entry_no) {
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
Database entry;
|
|
|
|
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
|
|
|
|
if ( i == entry_no ) {
|
|
|
|
printf("Entry found!\n");
|
2016-04-14 18:51:45 +00:00
|
|
|
fseek(file, -sizeof(Database), SEEK_CUR);
|
2016-04-14 18:34:42 +00:00
|
|
|
modify_entry(&entry);
|
|
|
|
fwrite(&entry, sizeof(Database), 1, file);
|
2016-04-14 18:51:45 +00:00
|
|
|
printf("RETSwrote %s\n", entry.repair_type);
|
2016-04-14 18:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 18:28:43 +00:00
|
|
|
|
2016-04-14 17:29:01 +00:00
|
|
|
void insert_entry(FILE * file, Database * entry) {
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
fwrite(entry, sizeof(Database), 1, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_entry(FILE * file, Database * entry) {
|
|
|
|
fread(entry, sizeof(Database), 1, file);
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:28:43 +00:00
|
|
|
void display_entry(Database * entry, unsigned index) {
|
|
|
|
printf("|%3d|%50s|%10s|%6d| %10d| %10d|\n",
|
|
|
|
index,
|
2016-04-14 17:29:01 +00:00
|
|
|
entry->repair_type,
|
|
|
|
entry->imei,
|
|
|
|
entry->price,
|
|
|
|
entry->investment,
|
2016-04-14 18:28:43 +00:00
|
|
|
entry->profit
|
2016-04-14 17:29:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_header() {
|
2016-04-14 18:28:43 +00:00
|
|
|
printf("|%3s|%50s|%10s|%6s| %10s| %10s|\n",
|
2016-04-14 17:29:01 +00:00
|
|
|
"i", "Repair Type", "IMEI", "Price", "Investment",
|
2016-04-14 18:28:43 +00:00
|
|
|
"Profit");
|
2016-04-14 17:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_database_entries(FILE * file) {
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
Database entry;
|
2016-04-14 18:28:43 +00:00
|
|
|
display_header();
|
|
|
|
|
|
|
|
long int total_profit = 0;
|
|
|
|
long int total_influx = 0;
|
|
|
|
|
|
|
|
for ( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
|
|
|
|
display_entry(&entry, i);
|
|
|
|
total_profit += entry.profit;
|
|
|
|
total_influx += entry.price;
|
2016-04-14 17:29:01 +00:00
|
|
|
}
|
2016-04-14 18:28:43 +00:00
|
|
|
|
|
|
|
printf("TOTAL PROFIT: %ld TOTAL INFLUX: %ld \n", total_profit, total_influx);
|
2016-04-14 17:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_menu() {
|
|
|
|
printf("\n");
|
|
|
|
printf("0. Exit program\n");
|
|
|
|
printf("1. Display the database.\n");
|
|
|
|
printf("2. Add an entry.\n");
|
|
|
|
printf("3. Update an entry.\n");
|
|
|
|
printf("4. Delete an entry.\n");
|
|
|
|
printf("5. Search by IMEI\n");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2016-04-14 15:27:49 +00:00
|
|
|
int main(void) {
|
2016-04-14 17:29:01 +00:00
|
|
|
FILE * fp = fopen("data.bdb", "r+b");
|
2016-04-14 18:28:43 +00:00
|
|
|
// for (int i = 0; i < 10; i++) {
|
|
|
|
// Database nokia = {
|
|
|
|
// .blacklisted = 6,
|
|
|
|
// .repair_type = "BATERIE3",
|
|
|
|
// .imei = "nuavema",
|
|
|
|
// .price = i,
|
|
|
|
// .investment = 2,
|
|
|
|
// .profit = 3,
|
|
|
|
// };
|
2016-04-14 17:29:01 +00:00
|
|
|
// insert_entry(fp, &nokia);
|
2016-04-14 18:28:43 +00:00
|
|
|
// }
|
|
|
|
Database e;
|
2016-04-14 18:51:45 +00:00
|
|
|
// read_entry(fp, &e);
|
2016-04-14 18:28:43 +00:00
|
|
|
display_database_entries(fp);
|
2016-04-14 18:51:45 +00:00
|
|
|
update_entry(fp, 2);
|
2016-04-14 18:28:43 +00:00
|
|
|
display_database_entries(fp);
|
2016-04-14 18:34:42 +00:00
|
|
|
// delete_entry(fp, 1);
|
|
|
|
// display_database_entries(fp);
|
2016-04-14 17:29:01 +00:00
|
|
|
// printf("after ins %ld\n", ftell(fp));
|
|
|
|
// fseek(fp, -sizeof(Database), SEEK_CUR);
|
|
|
|
// printf("before read %ld\n", ftell(fp));
|
|
|
|
// read_entry(fp, &e);
|
|
|
|
// printf("after read %ld\n", ftell(fp));
|
|
|
|
// printf("%s\n", e.repair_type);
|
2016-04-14 15:27:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|