1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include "hardware.h"
using namespace std;
int fileOpen(fstream& file, Hardware records[], int& count)
{
file.open("hardware.dat");
char input;
bool truth = false;
if(file.fail())
{
cout << "File cannot be opened. Create a new empty file" << endl;
file.open("hardware.dat", ios::out);
file.open("hardware.dat", ios::in | ios::out | ios::binary);
file.clear();
file.seekg(0, file.beg);
for (int i = 0; i < 100; i++) //Writes into the binary file, making it empty
file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
return 1; //If a new file is made
}
else if(file.good())
{
do { cout << "Should the file be initialised (Y or N): ";
cin >> input;
if (input == 'Y' || input == 'y') //Clears Binary File
{
file.open("hardware.dat", ios::out);
file.open("hardware.dat", ios::in | ios::out | ios::binary);
file.clear();
file.seekg(0, file.beg);
for (int i = 0; i < 100; i++)
file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
cout << "Yes" << endl;
truth = true;
}
else if (input == 'N' || input == 'n') //Reads from existing binary file
{
for (int i = 0; i < 100; i++)
{
file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
if (records[i].num > 0)
count++;
}
cout << "No" << endl;
truth = true;
}
else
{
cout << "Invalid Input" << endl;
truth = false;
}
} while(truth == false);
return 2; //If there is an existing file
}
}
void newInput(fstream& file, Hardware records[], int& count)
{
//At the beginning of every function, read from the binary file
for (int i = 0; i < 100; i++)
{
file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
}
int numLoop = 0;
for (int i = 0; i < 100; i++)
{
bool truth = false;
cout << "Enter the part number (0-99, -1 to end the input): ";
do {
cin >> numLoop;
if(numLoop > 99)
{
cout << "Invalid Input. Try Again" << endl;
truth = false;
}
else
{
truth = true;
}
} while(truth == false);
if (numLoop < 0)
break;
else
records[i].num = numLoop;
cin.ignore();
cout << "Enter the tool name: ";
cin.get(records[i].name, 30, '\n');
cin.ignore();
cout << "Enter the quantity: ";
cin >> records[i].quantity;
cin.ignore();
cout << "Enter the price: ";
cin >> records[i].price;
count++;
}
for (int i = 0; i < count; i++)
file.write(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
}
void output(fstream& file, Hardware records[], int count)
{
for (int i = 0; i < count; i++)
file.read(reinterpret_cast<char *>(&records[i]), sizeof(Hardware));
file.clear();
file.seekg(0, file.beg);
int location = file.tellg();
cout << "Current Location: " << location << endl;
for (int i = 0; i < count; i++)
{
cout << count << '\t' << records[i].num << '\t' << records[i].name << '\t';
cout << records[i].quantity << '\t' << records[i].price << endl;
}
}
|