Sorry, this is going to be a very long post :$
first of all, here is my codes:
Header File
struct hardware
{
int record;
char name[100];
int quantity;
float cost;
};
Binary Input File cpp
#include<iostream>
#include<iomanip>
#include<fstream>
#include <cstring>
#include "hardware.h"
using namespace std;
void get_data(hardware *);
void show_hardware();
fstream emp_file; // global fstream object
void main()
{
hardware temp;
emp_file.open("empfile.bin", ios::in | ios::out | ios::trunc | ios::binary);
if (!emp_file)
{
cout << "\nCannot open hardware file";
exit(1);
}
cout << "Enter some lines of hardware data\n"
<< "(Enter Ctrl-Z to stop)\n\n";
while (!cin.eof() )
{
get_data(&temp);
if(!cin.eof() )
{
emp_file.write( (char *)&temp, sizeof(hardware) );
}
}
show_hardware();
emp_file.close();
cout << "\n\n";
}
void get_data(hardware *pwkr)
{
static int i = 0;
int j;
i++;
for(j = 0; j < 20; j++) pwkr->name[j] = ' '; // set array to spaces
cout << "Enter the Record#, "
<<"Tool Name, Quantity and the Cost "
<<"for Hardware #" << i << " : ";
cin >> pwkr->record>> pwkr->name >> pwkr->quantity >> pwkr->cost;
}
void show_hardware()
{
int i = 0;
hardware temp;
cout << "\n\nThese are the Hardware Tools in the System\n"
<< setiosflags(ios::showpoint | ios::fixed);
cout << "Tool\tRecord# Tool Name\tQuantity\tCost";
emp_file.seekg(0,ios::beg);
emp_file.read( (char *)&temp, sizeof(hardware) );
while ( !emp_file.eof() )
{
i++;
cout << "\n" << i << "\t"
<< temp.record << "\t"
<< temp.name << "\t\t"
<< temp.quantity << "\t\t "
<< setprecision(2)
<< temp.cost;
emp_file.read( (char *)&temp, sizeof(hardware) );
}
}
Binary Direct Input & Output cpp
#include<iostream>
#include<iomanip>
#include<fstream>
//#include <process.h>
#include <cstring>
#include "hardware.h"
using namespace std;
void set_money_display(void);
int retrieve_hardware(hardware *);
void alter_data(hardware *, int);
void update_hardware(hardware *, int);
void show_hardware();
fstream emp_file ("empfile.bin", ios::in | ios::out | ios::binary );
void main()
{
char choice;
hardware temp;
int posn;
set_money_display();
if (!emp_file)
{
cout << "Cannot open file - empfile.bin";
exit(1);
}
show_hardware();
cin.get();
posn = retrieve_hardware(&temp);
alter_data(&temp, posn);
cout << "\nDo you want to store these changes? ";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
update_hardware(&temp, posn);
}
show_hardware();
emp_file.close();
cout << "\n\n";
}
void set_money_display()
{
cout << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint);
}
int retrieve_hardware(hardware *ptemp)
// get the data of an hardware in the file
{
int posn;
cout << "\n\nEnter the position number of a hardware in the file\n";
cin >> posn;
cin.get();
posn --;
emp_file.clear();
emp_file.seekg(posn * sizeof(hardware),ios::beg);
emp_file.read( (char *)ptemp, sizeof(hardware) );
return(posn);
}
void alter_data(hardware *ptemp, int posn)
//get user to key in new hardware details
{
char tname[20];
cout << "Enter new details for hardware " << (posn + 1) << "\n";
cout << "Name: " << ptemp->name << " ";
cin.getline(tname, 19);
if (tname[0] != '\0') // a new name was input
{
strcpy(ptemp->name,tname);
}
cout << "Record#: " << setw(2) << ptemp->record << " ";
cin >> ptemp->record;
cout << "Quantity: " << setw(2) << ptemp->quantity << " ";
cin >> ptemp->quantity;
cout << "Cost: " << setw(2) << ptemp->cost << " ";
cin >> ptemp->cost;
}
void update_hardware(hardware * ptemp, int posn)
// overwrite the old hardware details in the file
{
emp_file.seekp(posn * sizeof(hardware),ios::beg);
emp_file.write( (const char *)ptemp, sizeof(hardware) );
}
void show_hardware()
{
int i = 0;
hardware temp;
cout << "\n\nThese are the hardware in the file\n"
<< setiosflags(ios::showpoint | ios::fixed);
cout << "Hardwar\tRecord# Tool Name\tQuantity\tCost";
emp_file.seekg(0,ios::beg);
emp_file.read( (char *)&temp, sizeof(hardware) );
while ( !emp_file.eof() )
{
i++;
cout << "\n" << i << "\t"
<< temp.record <<"\t"
<< temp.name << "\t\t"
<< temp.quantity << "\t\t"
<< setprecision(2)
<< temp.cost << " ";
emp_file.read( (char *)&temp, sizeof(hardware) );
}
}
now here is my question and concerns:
1) i need to be able to input a list of hardware tools and for each hardware there needs to include these informations: (record #, name, quantity and cost).
this part is done, i was able to do it after i used the Binary Input File cpp.
a)but there is a problem when i try to enter the name.
for example: if i enter Screw Driver(with space then the program will crash. but if i enter Screw_Driver(with underscore) then i am ok. ???
2) after i enter a few hardware tools i need to able to update the information or delete it if i want to.
so i used Binary Direct Input and Output cpp.
Sample output when i run Binary Direct Input and Output
Hardware Record# Tool Name Quantity Cost
1 68 Screw_Driver 106 6.99
2 83 Wrench 34 7.50
two issues here
a)i am able to update any information i want but i need to refer to the hardware number(1,2..). how do i change my code so i can refer to the record # of the hardware if i want to change/update a hardware information?
b)how do i delete a hardware complete?
3)lastly, where do i go if i want to check what is being stored after i used Binary Input File cpp. is there suppose to be a .DAT file created after i compile the Binary Input File?
i check my folder where these cpp and header files are located but there is none.
thanks.