Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define PASS1 "daniel"
#define PASS2 "pword24"
#define INVARY 10
#define TRANS 1000
struct inventory {
char desc[25];
int invCode;
int invAmount;
float invPrice;
};
struct transaction {
char desc[25];
int invCode;
int invAmount;
float invPrice;
};
void printInfo(struct inventory inv_db_new);
FILE * disk_file;
main()
{
//Variables
int counter;
//Create arrays of the stuctures
struct inventory inv_db[INVARY] = {{"Widget", 1001, 10, 2.95},
{"Widget2", 1002, 10, 5.20},
{"Widget3", 1003, 10, 10.00},
{"Widget4", 1004, 10, 4.56},
{"Widget5", 1005, 10, 9.99},
{"Widget6", 1006, 10, 4.44},
{"Widget7", 1007, 10, 70.02},
{"Widget8", 1008, 10, 45.66},
{"Widget9", 1009, 10, 34.55},
{"Widget10", 1010, 10, 30.23}};
struct inventory inv_db_new[INVARY];
struct inventory aVar;
struct transaction trans_db[TRANS];
disk_file = fopen("c:\\may.inv", "w+");
if (!disk_file) {
printf("Error, with disk\n");
}
printf("Success, with disk\n");
fwrite(inv_db, sizeof(inv_db), 1, disk_file);
printf("Success in writing to disk\n");
fclose(disk_file);
disk_file = fopen("c:\\may.inv", "r+");
if (!disk_file) {
printf("Error, with disk\n");
}
fread(inv_db_new, sizeof(inv_db_new), 1, disk_file);
for (counter = 0; counter < INVARY; counter++) {
printInfo(inv_db_new[counter]);
}
fclose(disk_file);
disk_file = fopen("c:\\may.inv", "r+");
if (!disk_file) {
printf("Error, with disk\n");
}
fseek(disk_file, (4L * sizeof(aVar)), SEEK_SET);
fread(&aVar, sizeof(aVar), 1, disk_file);
printInfo(aVar);
fclose(disk_file);
}
void printInfo(struct inventory str_nam)
{
printf("\n%d ", str_nam.invCode);
printf("%s ", str_nam.desc);
printf("%d ", str_nam.invAmount);
printf("$%.2f\n", str_nam.invPrice);
return;
}