I'm taking an advanced C++ class now, and everything up to now has been pretty simple (mostly review stuff), but now we got to binary file io and it's kicking my butt.
This program is supposed to read from a file called "Hardware.dat" (or create it if it doesn't exist), and allow the user to manipulate the data by adding a new record, modifying an existing record, deleting a record, and display all the records. Each record contains the record number, name, quantity, cost, and a flag telling if it's active or not.
Adding a record is no problem. Modifying shows the record it's supposedly modifying, asks for input, but does nothing to actually modify the record. Deleting is also supposed to show the record your deleting, but instead, it throws this jargon at me:
Record: 1632108544 Name: mmer Quantity: 1030815744 Cost: 2.2
The record in question was this:
Record: 3 Name: Hammer Quantity: 51 Cost: 12
Naturally, it does nothing in the way of deleting the record either.
Printing the records is okay, though somehow I broke the functionality I had that allowed it to print sequentially, like if you entered record 8, then 3, then 10, it would print 3, 8, 10. Now, it just prints in the order you entered, regardless of number.
With that out of the way, here's the code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
class tool
{
private:
int record;
char name[30];
int qty;
double cost;
public:
//bool active;
tool(int=0);
void getInput(fstream&, tool, bool, int);
void display(bool);
};
tool::tool(int rec)
{
record = rec;
}
int main()
{
bool active = false;
int number = 0;
int again = 1;
int menu = 0;
tool t1;
fstream fio;
fio.open("hardware.dat", ios::out | ios::in | ios::binary | ios::app);
if(!fio)
{
fio.open("hardware.dat", ios::out);
cout << "file has been created, run the program again to start inputting tools." << endl;
}
else
{
fio.seekp(0, ios::end);
cout << "Operation Menu: \n 1. Add a record \n 2. Modify a record \n 3. Delete a record \n 4. List records \n 5. Quit" << endl;
while (menu != 5)
{
cout << "\nEnter a choice ";
cin >> menu;
switch(menu)
{
case 1:
{
t1.getInput(fio, t1, false, 0);
active = true;
fio.write((char*)(&t1), sizeof(t1));
fio.write((char*)(&active), sizeof(active));
break;
}
case 2:
{
cout << "Enter record number to modify";
cin >> number;
fio.seekg((number + 1) * sizeof(t1));
fio.read((char*)(&t1), sizeof(t1));
cout << "You are modifying this record:" << endl << endl;
t1.display(true);
fio.seekp((number + 1) * sizeof(t1));
t1.getInput(fio, t1, true, number);
active = true;
fio.write((char*)(&t1), sizeof(t1));
fio.write((char*)(&active), sizeof(active));
break;
}
case 3:
{
cout << "Enter record number to delete";
cin >> number;
fio.seekg(number + 1 * sizeof(t1));
fio.read((char*)(&t1), sizeof(t1));
t1.display(active);
fio.seekp(number + 1 * sizeof(t1));
active = false;
fio.write((char*)(&t1), sizeof(t1));
fio.write((char*)(&active), sizeof(active));
cout << "Has been deleted" << endl;
break;
}
case 4:
{
fio.seekg(0);
fio.read((char*) (&t1), sizeof(t1));
fio.read((char*)(&active), sizeof(active));
while(fio)
{
t1.display(active);
fio.read((char*)(&t1), sizeof(t1));
fio.read((char*)(&active), sizeof(active));
}
break;
}
case 5:
{
break;
}
default:
{
break;
}
}
fio.seekg(0);
fio.seekp(0);
}
}
system("pause");
return 0;
}
void tool::getInput(fstream& fio, tool t1, bool modify, int number)
{
if(modify == false)
{
int begin, end = 0;
cout << "Enter record number. ";
cin >> record;
fio.seekp(record * sizeof(t1));
}
else
record = number;
cin.ignore(100, '\n');
cout << "Enter the name of the tool. ";
cin.getline(name, 30);
cout << "Enter the quantity of the tool. ";
cin >> qty;
cout << "Enter the cost of the tool. ";
cin >> cost;
}
void tool::display(bool active)
{
if (active == 1)
cout << left << setw(8) << "Record: " << setw(3) << record << " Name: " << setw(30) << name << " Quantity: " << setw(4) << qty << " Cost: " << setw(8) << setprecision(2) << cost << endl;
}
It's probably a mess, I've been changing stuff around trying to get it to work, to no avail. Hopefully someone can point me in the right direction, I just can't figure out how to make it work. Thanks in advance.