After creating a basic/bogues record, I open the .dat file directly with Microsoft notepad, and I recognize that there contents indeed added to the file.
Back to the program, I can't seem to have the contents of binary file display on the screen properly.
Using the following statement, with ios::in included: fstream dataFile("inventory.dat", ios::out | ios::binary);
Will allow me call/select the, void display(), and void allContent(), functions, but inventory.dat file won't be created at run time.
Using fstream dataFile("inventory.dat", ios::out | ios::binary);
Would require me to declare a "dFile2", fstream variable, but void display and void allContent (to display every record at once), will result in a runtime error, when they are actually selected from the menu.
The author's closes example to this program, showed him first using a for loop to initialize 5 empty records. The directions to this assignment calls for no such thing.
Directions:
Quote
• Add new records to the file.
• Display any record in the file.
• Change any record in the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Items
{
string desc;
int quantity;
int wholeCost, retailCost;
string dateAdded;
};
void options();
void addNew(fstream&);
void display(fstream&);
void allContent(fstream&);
//void change();
int main()
{
//Items call;
int option;
//fstream dataFile("inventory.dat", ios::out | ios::binary);
fstream dataFile("inventory.dat", ios::out | ios::in | ios::binary); // ::in, won't allow program to create a blank .dat file on its own
//Items record = {" ", 0, 0, 0, " "};
//dataFile.write(reinterpret_cast<char *>(&record),sizeof(record)); //writes blank record
do
{
options();
cout << "Your selection: ";
cin >> option;
cout << endl;
switch (option)
{
case 1:
addNew(dataFile);
break;
case 2:
display(dataFile);
break;
case 4:
allContent(dataFile);
break;
case 3:
//change();
break;
}
} while (option > 0 && option < 5);
cout << "\nBye-bye.\n";
dataFile.close();
system("pause");
return 0;
}
void options()
{
cout << "\nPress 1, to add a new record.\n";
cout << "Press 2, to display current records.\n";
cout << "Press 3, to change a record.\n";
cout << "Press 4, to view all contents.\n";
cout << "Press 0, to quit.\n\n";
}
void addNew(fstream& dFile)
{
//fstream dFile2 ("Inventory.dat", ios::out | ios::binary);
Items call;
cout << "Enter Description: ";
cin.ignore(); //must be executed atleast once to allow user input later.
getline(cin, call.desc); //use this instead of cin >>, so content after spaces can be recognized
//cin.getline(call.desc); //
cout << "Quantity : ";
cin >> call.quantity;
cout << "Wholesale cost : ";
cin >> call.wholeCost;
cout << "Retail cost : ";
cin >> call.retailCost;
cout << "Data added : ";
cin.ignore();
getline(cin, call.dateAdded);
cout << "Adding record to file...\n";
// prevent new records from over-writing old records.
dFile.seekp(ios::end);
dFile.write(reinterpret_cast<char *>(&call),sizeof(call));
cout << "size of record " << sizeof(call) << endl;
dFile.close();
}
void display(fstream& dFile)
{
//fstream dFile("Inventory.dat", ios::in); //this produces an error message.
//fstream dFile2("Inventory.dat", ios::in | ios::binary); //infinite loop & compile time error
Items call;
int recNum;
cout << "Enter the item's record number to view: ";
cin >> recNum;
dFile.seekg(recNum * sizeof(call), ios::beg);
dFile.read(reinterpret_cast<char *>(&call),sizeof(call));
cout << "Description : " << call.desc << endl;
cout << "Quantity : " << call.quantity << endl;
cout << "Wholesale cost: " << call.wholeCost << endl;
cout << "Retail cost : " << call.retailCost << endl;
cout << "Date : " << call.dateAdded << endl;
}
void allContent(fstream& dFile)
{
Items call;
//fstream dFile2("inventory.dat", ios::in | ios:: binary);
// Now read and display ALL the records
dFile.read(reinterpret_cast<char *>(&call), sizeof(call));
while ( ! dFile . eof() )
{
cout << "Description : " << call.desc << endl;
cout << "Quantity : " << call.quantity << endl;
cout << "Wholesale cost: " << call.wholeCost << endl;
cout << "Retail cost : " << call.retailCost << endl;
cout << "Date : " << call.dateAdded << endl;
dFile.read(reinterpret_cast<char *>(&call), sizeof (call) ) ;
}
dFile.close();
}

New Topic/Question
Reply



MultiQuote







|