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.

Recommended Answers

All 4 Replies

anyone?

Well, there are many comments to be made.

In your Binary Input file, you open the file for in/out and you truncate it. So, anything that was in it before is lost. Is that your intent?

Your console input problem with multiword names is due to using the extraction operator ( cin >> name), which will only read in one "word". You've found one solution, to use underscores or dashes in names. If you enter each data item, followed by newline, then you could use getline( ) to grab the string data.

Some general nitpicks -
Use int main( ) , not void main( ) . And be sure to end main( ) with a return 0;

Use of global variables (your fstream object) is generally frowned upon. Declare it in main and pass it (by reference) to the function that needs it. Or declare it and open it in that function.

You're writing C++, why not pass variable by reference in the C++ fashion:

void get_data(hardware &);  //now use the dot operator instead of pointer arrow
......
get_data( temp );

As to how to find/edit a record based on its record number, how about keeping a table correlating position in file to record numbers as you read through the file? Then you have an index that allows quicker access to any record.

In your Binary Input file, you open the file for in/out and you truncate it. So, anything that was in it before is lost. Is that your intent?

no, how should i fix it, i want the code to create a dat file so i can see what is being store after i use it. i check the folder where the cpp file is store and there is not a dat file?

Use of global variables (your fstream object) is generally frowned upon. Declare it in main and pass it (by reference) to the function that needs it. Or declare it and open it in that function.

You're writing C++, why not pass variable by reference in the C++ fashion:

void get_data(hardware &);  //now use the dot operator instead of pointer arrow
......
get_data( temp );

you mean like this

int main()
{
fstream emp_file;
}
....

As to how to find/edit a record based on its record number, how about keeping a table correlating position in file to record numbers as you read through the file? Then you have an index that allows quicker access to any record.

can you show a example, i am new to files.

thanks

This should address some of my points. If no one chimes in with more, I'll address it more fully later - gotta run to an exam right now.

#include <fstream>
#include <iostream>
using namespace std;

struct hardware
{
	int record;
   char name[100];
   int quantity;
   float cost;
};

int reader( fstream &r, hardware data[], int max );

int main( )
{
   hardware parts[50] = { 0 };
   int count = 0;
   int i;
   int pn;

   fstream in_out;

   in_out.open( "parts.txt" , ios::in | ios::out | ios::ate | ios::binary );
   if( !in_out )
   {
      cout << "error opening file, exiting." <<endl;
      return -1;
   }

   count = reader( in_out, parts, 50 );

   for( i = 0; i < count; i++ )
      cout << i << "\t" << parts[i].record << endl;

   //lookup by part_num
   cout << "Part number to look up: " ;
   cin >> pn;

   for( i = 0; i < count && pn != parts[i].record; i++ )
      ;

   if( i < count )
      //display or otherwise process part at index i
      ;
   else
      cout << "Part number " << pn << " not found." <<endl;

   return 0;
}


int reader( fstream &r, hardware data[], int max )
{
    int i = 0;
    hardware temp;

    r.seekg(0,ios::beg);
 
    while ( r.read( (char *)&temp, sizeof(hardware) ) )
      	i++;

    return i;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.