Structure record binary file, refuses to display contents.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 5261 Views - Last Post: 30 September 2012 - 10:21 AM Rate Topic: -----

#16 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 12:08 PM

Thanks for the heads up, I copied an pasted it similar code, without taking to the time proof read.

I implemented a mock, while loop (line 34), to try and display all the records at once, but it instead only displayed the 1st record after each iteration of the loop (in this case 3 times.

void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	size_t descSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&descSize), sizeof(size_t));
	// resize string to the correct size
	call.desc.resize(descSize);
	// read in the string
	dFile.read(&call.desc[0], descSize);
	
	// read the quantity, whole & retail cost.
	dFile.read(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	dFile.read(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.read(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	size_t dateSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&dateSize), sizeof(size_t));
	// resize string to the correct size
	call.dateAdded.resize(dateSize);
	// read in the string
	dFile.read(&call.dateAdded[0], dateSize);


	//dFile.seekg(recNum * sizeof(call), ios::beg);  
	//dFile.read(reinterpret_cast<char *>(&call),sizeof(call));  

	Items show;
	int i = 0;
	while ( ! dFile . eof() && i < 4 ) 
	{
	cout << "Description\t: " << call.desc << endl;  
	cout << "Quantity\t: " << call.quantity << endl;  
	cout << "Wholesale cost\t: " << call.wholeCost << endl;  
	cout << "Retail cost\t: " << call.retailCost << endl;  
	cout << "Date\t\t: " << call.dateAdded << endl;  
	i++;
	}

	dFile.close();
}



I'm wondering for the 2nd and the 3rd records, should I implement different instances with vector, then use a for loop to display them?
Was This Post Helpful? 0
  • +
  • -

#17 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 12:23 PM

Your problem is that you only read one record, so it can only print one record. If you want to display more than one record you will need to read more than one record.

I keep telling you to move your code that actually reads the file into it's own function. It will make reusing this code much easier. Eventually you will need this code in other places than just the display function. You also need to do this with the code that actually writes the file, you will probably also need this in more than just one function.

Jim
Was This Post Helpful? 0
  • +
  • -

#18 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 01:32 PM

May you or someone else please point to where in the display function, did I specify to read only 1 records instead of all the records?

void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	//readFile(dFile, call); Produces error message
	size_t descSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&descSize), sizeof(size_t));
	// resize string to the correct size
	call.desc.resize(descSize);
	// read in the string
	dFile.read(&call.desc[0], descSize);
	
	// read the quantity, whole & retail cost.
	dFile.read(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	dFile.read(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.read(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	size_t dateSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&dateSize), sizeof(size_t));
	// resize string to the correct size
	call.dateAdded.resize(dateSize);
	// read in the string
	dFile.read(&call.dateAdded[0], dateSize);

	Items show;
	int i = 0;
	while ( ! dFile . eof() && i < 4 ) 
	{
	cout << "Description\t: " << call.desc << endl;  
	cout << "Quantity\t: " << call.quantity << endl;  
	cout << "Wholesale cost\t: " << call.wholeCost << endl;  
	cout << "Retail cost\t: " << call.retailCost << endl;  
	cout << "Date\t\t: " << call.dateAdded << endl;  
	i++;
	}

	dFile.close();
}



I tried implementing the read function you suggested, and well, I had no luck. I created it as a void.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 01:36 PM

Quote

I tried implementing the read function you suggested, and well, I had no luck. I created it as a void.

Why didn't you have any luck? Why didn't it work? Did you even try calling this new function?

If you got error messages then post the complete error messages exactly as they appear in your development environment.


Jim
Was This Post Helpful? 0
  • +
  • -

#20 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 01:57 PM

It was foolish mistake on my part, I missed an ampersand in the function header.
So, display, has the read function working now.

But now I'm still wondering about where in that function (and read function as well) did I specify, the program only to read/display 1 record, when the .dat/binary file, cleary has multiple records in it?

.dat file

Quote

Trick Bike  &  02252003
Mountain Bike  4  02292005 Unicycle    03232005


Note: Only trick bike's record is displayed, 4 times, as well, due to the (mock) while loop.
Please note some symbols from the .dat file are unable to be pasted onto this forum post/forum software. Also when i start the program, I can go straight to selection 2, without having the program crash.

Full program
#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 &, Items &);
void display(fstream &, Items &);
void allContent(fstream &);
bool fileExists(string);
void readFile(fstream &, Items &);
//void change();

int main()
{
	Items call;
	int option;

	fstream dataFile;//("inventory.dat", ios::out | ios::binary);
	//fstream dataFile("inventory.dat", ios::in | 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:
			dataFile.open("inventory.dat", ios::out | ios::binary | ios::app);
			addNew(dataFile, call);
			dataFile.close();
			break;
		case 2:
			dataFile.open("inventory.dat", ios::in | ios::binary);
			if(!fileExists("inventory.dat"))
			{
				cout << "File is not opened.\n";
				system("pause");
				return 0;
			}
			
			display(dataFile, call);
			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, Items &call)
{
	//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\t: ";
	cin >> call.quantity;
	cout << "Wholesale cost\t: "; 
	cin >> call.wholeCost;
	cout << "Retail cost\t: ";
	cin >> call.retailCost;
	cout << "Data added\t: "; 
	cin.ignore();
	getline(cin, call.dateAdded);

	cout << "Adding record to file...\n";  

	dFile.seekp(ios::end); 
	// first obtain size of the structure's string desc member vairable
	size_t sDescSize = call.desc.size();
	dFile.write(reinterpret_cast<char*>(&sDescSize), sizeof(size_t));
	// write actual string (descripton)
	dFile.write(call.desc.c_str(), sDescSize);

	// write out quantity member vairable
	dFile.write(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	// write out wholeCost and retailCost
	dFile.write(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.write(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	// obtain size of the structure's string date member vairable
	size_t sDateSize = call.dateAdded.size();
	dFile.write(reinterpret_cast<char*>(&sDateSize), sizeof(size_t));
	// write actual string (date added)
	dFile.write(call.dateAdded.c_str(), sDateSize);


	// prevent new records from over-writing old records.
	/*dFile.seekp(ios::end); 
	dFile.write(reinterpret_cast<char *>(&call),sizeof(call));
	//dFile.write(reinterpret_cast<char *>(&call),sizeof(call));  

	cout << "size of record " << sizeof(call) << endl;*/

	dFile.close();	//I've placed this in case 1 within the do-while loop of int main.
}


void readFile(fstream & dFile, Items &call)
{
	size_t descSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&descSize), sizeof(size_t));
	// resize string to the correct size
	call.desc.resize(descSize);
	// read in the string
	dFile.read(&call.desc[0], descSize);
	
	// read the quantity, whole & retail cost.
	dFile.read(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	dFile.read(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.read(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	size_t dateSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&dateSize), sizeof(size_t));
	// resize string to the correct size
	call.dateAdded.resize(dateSize);
	// read in the string
	dFile.read(&call.dateAdded[0], dateSize);

}


void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	readFile(dFile, call); //Produces error message

	Items show;
	int i = 0;
	while ( ! dFile . eof() && i < 4 ) 
	{
	cout << "Description\t: " << call.desc << endl;  
	cout << "Quantity\t: " << call.quantity << endl;  
	cout << "Wholesale cost\t: " << call.wholeCost << endl;  
	cout << "Retail cost\t: " << call.retailCost << endl;  
	cout << "Date\t\t: " << call.dateAdded << endl;  
	i++;
	}

	dFile.close();
}



void allContent(fstream& dFile)	//Display all/present contents at once
{
	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();
}




bool fileExists(string filename)
{
		fstream file(filename, ios::in | ios::binary);
		bool exist = true;

		if( !file.is_open()) 
		{
			exist = false;
		}

		file.close();
		return exist;
}

This post has been edited by mgrex: 28 September 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 02:10 PM

Quote

But now I'm still wondering about where in that function (and read function as well) did I specify, the program only to read/display 1 record, when the .dat/binary file, cleary has multiple records in it?

Now since you have moved the read to it's own function and display looks like:
void display(fstream& dFile, Items & call)
{
	readFile(dFile, call);

	Items show; // NOT NEEDED
	int i = 0; // Neither is this. Just use while(dFile).
	while ( ! dFile . eof() && i < 4 ) 
	{
	cout << "Description\t: " << call.desc << endl;  
	cout << "Quantity\t: " << call.quantity << endl;  
	cout << "Wholesale cost\t: " << call.wholeCost << endl;  
	cout << "Retail cost\t: " << call.retailCost << endl;  
	cout << "Date\t\t: " << call.dateAdded << endl;  
	i++;
	}

	dFile.close();
}


Since the readFile() function reads only one record at a time please show me where you are reading more than one record.

You don't use the your variable show so you should get rid of it. You also don't need i, if you want to display all the records in your file.

Also, don't close the file in this function. Close the file in the function where you opened the file.

Jim

This post has been edited by jimblumberg: 28 September 2012 - 02:11 PM

Was This Post Helpful? 1
  • +
  • -

#22 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 02:31 PM

I moved the dFile.close(); statement to the case 2 statement, where I opened the file.

Using .eof() in the while statement: while ( ! dFile . eof() )
void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	readFile(dFile, call); //Produces error message

	int i = 0;
	while ( ! dFile . eof() ) 
	{
		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  
	i++;
	}

	cout << "total iter: " << i << endl;
 
}


An infinite loop results, of just displaying the Trick Bike record over and over again. I have left the int i statement, as a way to help with error diagnosis for now.


Also I found that by duplicating statements of display function, the other records were able to dispaly.

void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	readFile(dFile, call); //Produces error message

	//int i = 0;
	//while ( ! dFile . eof() ) 
	//{


		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  
	//i++;
	//}

	//cout << "total iter: " << i << endl;
 

		readFile(dFile, call);

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  



		readFile(dFile, call);

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  
}



The program didn't crash with that.

I tried the following, which result in a run-time error, after pressing 2.
	while (  !dFile . eof() ) 
	{
		readFile(dFile, call);

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  
	//i++;
	}

This post has been edited by mgrex: 28 September 2012 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 02:36 PM

Look closely at your duplication. What does all this duplication have in common?

Doesn't the following appear in every occurance?
		readFile(dFile, call);

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl;  


What did your previous code with the while leave out? With your duplication how many times are you calling readfile()? How many times did you call readfile() in your while version?????

Maybe you should consider changing the name of readFile() to readRecord(), maybe that might ring some bells.



Jim

This post has been edited by jimblumberg: 28 September 2012 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#24 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 02:49 PM

I failed to mention that with the while loop, all the records were successfuly displayed, but then a pop up error appears stating:

Quote

Unhandled exception at 0x75dab727 in 12_13_Inventory.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0030e938..


void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	 //Produces error message

	int i = 0;
	
	while (  !dFile . eof() ) 
	{
		readFile(dFile, call);

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl << endl;  
	i++;
	}

	cout << "total iter: " << i << endl;
 
}



Full code
Spoiler

Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 02:56 PM

That is because you are using eof() to control your loop, but you read the file after you test the state and if this read fails then you will have problems with your cout statements.

I recommend changing your readFile() function to return a bool and then use the read function to control your loop.

bool readFile(fstream & dFile, Items &call)
{
	size_t descSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&descSize), sizeof(size_t));
	// resize string to the correct size
	call.desc.resize(descSize);
	// read in the string
	dFile.read(&call.desc[0], descSize);
	
	// read the quantity, whole & retail cost.
	dFile.read(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	dFile.read(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.read(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	size_t dateSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&dateSize), sizeof(size_t));
	// resize string to the correct size
	call.dateAdded.resize(dateSize);
	// read in the string
	dFile.read(&call.dateAdded[0], dateSize);
        return(dFile); // This will return a bool indicating the status of the stream.
}




	while ( readFile(dFile, call) ) 
	{
		

		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl << endl;  
	i++;
	}



Jim
Was This Post Helpful? 1
  • +
  • -

#26 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 03:35 PM

making readRecord (was readFile), a bool function, still resulted in the same error. All the records were displayed successfully, but then a fatal error resulted.

Quote

Unhandled exception at 0x75dab727 in 12_13_Inventory.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0038e888..



#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 &, Items &);
void display(fstream &, Items &);
void allContent(fstream &);
bool fileExists(string);
bool readRecord(fstream &, Items &);
//void change();

int main()
{
	Items call;
	int option;

	fstream dataFile;//("inventory.dat", ios::out | ios::binary);
	//fstream dataFile("inventory.dat", ios::in | 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:
			dataFile.open("inventory.dat", ios::out | ios::binary | ios::app);
			addNew(dataFile, call);
			dataFile.close();
			break;
		case 2:
			dataFile.open("inventory.dat", ios::in | ios::binary);
			if(!fileExists("inventory.dat"))
			{
				cout << "File is not opened.\n";
				system("pause");
				return 0;
			}
			
			display(dataFile, call);
			dataFile.close();
			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, Items &call)
{
	//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\t: ";
	cin >> call.quantity;
	cout << "Wholesale cost\t: "; 
	cin >> call.wholeCost;
	cout << "Retail cost\t: ";
	cin >> call.retailCost;
	cout << "Data added\t: "; 
	cin.ignore();
	getline(cin, call.dateAdded);

	cout << "Adding record to file...\n";  

	dFile.seekp(ios::end); 
	// first obtain size of the structure's string desc member vairable
	size_t sDescSize = call.desc.size();
	dFile.write(reinterpret_cast<char*>(&sDescSize), sizeof(size_t));
	// write actual string (descripton)
	dFile.write(call.desc.c_str(), sDescSize);

	// write out quantity member vairable
	dFile.write(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	// write out wholeCost and retailCost
	dFile.write(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.write(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	// obtain size of the structure's string date member vairable
	size_t sDateSize = call.dateAdded.size();
	dFile.write(reinterpret_cast<char*>(&sDateSize), sizeof(size_t));
	// write actual string (date added)
	dFile.write(call.dateAdded.c_str(), sDateSize);


	// prevent new records from over-writing old records.
	/*dFile.seekp(ios::end); 
	dFile.write(reinterpret_cast<char *>(&call),sizeof(call));
	//dFile.write(reinterpret_cast<char *>(&call),sizeof(call));  

	cout << "size of record " << sizeof(call) << endl;*/

	dFile.close();	//I've placed this in case 1 within the do-while loop of int main.
}


bool readRecord(fstream & dFile, Items &call)
{
	size_t descSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&descSize), sizeof(size_t));
	// resize string to the correct size
	call.desc.resize(descSize);
	// read in the string
	dFile.read(&call.desc[0], descSize);
	
	// read the quantity, whole & retail cost.
	dFile.read(reinterpret_cast<char*>(&call.quantity), sizeof(call.quantity));
	dFile.read(reinterpret_cast<char*>(&call.wholeCost), sizeof(call.wholeCost));
	dFile.read(reinterpret_cast<char*>(&call.retailCost), sizeof(call.retailCost));

	size_t dateSize;
	// Read the size of the string
	dFile.read(reinterpret_cast<char*>(&dateSize), sizeof(size_t));
	// resize string to the correct size
	call.dateAdded.resize(dateSize);
	// read in the string
	dFile.read(&call.dateAdded[0], dateSize);

	return (dFile);
}


void display(fstream& dFile, Items & call)
{
	//int recNum;
	//cout << "Enter the item's record number to view: ";
	//cin >> recNum;

	 //Produces error message

	int i = 0;
	
	while (readRecord(dFile, call)) //!dFile . eof() ) 
	{
		
		cout << "Description\t: " << call.desc << endl;  
		cout << "Quantity\t: " << call.quantity << endl;  
		cout << "Wholesale cost\t: " << call.wholeCost << endl;  
		cout << "Retail cost\t: " << call.retailCost << endl;  
		cout << "Date\t\t: " << call.dateAdded << endl << endl;  
		i++;
	}

	cout << "total iter: " << i << endl;
 
}



void allContent(fstream& dFile)	//Display all/present contents at once
{
	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();
}




bool fileExists(string filename)
{
		fstream file(filename, ios::in | ios::binary);
		bool exist = true;

		if( !file.is_open()) 
		{
			exist = false;
		}

		file.close();
		return exist;
}

Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,691
  • Joined: 25-December 09

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 03:37 PM

Where is the problem occurring? Run the program through your debugger and it will tell you exactly where the problem is detected.


Jim
Was This Post Helpful? 0
  • +
  • -

#28 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 28 September 2012 - 03:44 PM

Quote

Unhandled exception at 0x75dab727 in 12_13_Inventory.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0031e4dc..



The dbgheap.c file is quit large in Microsoft Visual Studio 2010, but this is the part keeps coming up.

The same error also happened when deleted the .dat/binary file and tried to display an empty file.

Starts at line 224 of 3268 of the file:
extern "C" static void * __cdecl _nh_malloc_dbg_impl (
        size_t nSize,
        int nhFlag,
        int nBlockUse,
        const char * szFileName,
        int nLine,
        int * errno_tmp
        )
{
        void * pvBlk;

        for (;;)/>
        {
            /* do the allocation
             */
            pvBlk = _heap_alloc_dbg_impl(nSize, nBlockUse, szFileName, nLine, errno_tmp);

            if (pvBlk)
            {
                return pvBlk;
            }
            if (nhFlag == 0)
            {
                if (errno_tmp)
                {
                    *errno_tmp = ENOMEM;
                }
                return pvBlk;
            }

            /* call installed new handler */
            if (!_callnewh(nSize))
            {
                if (errno_tmp)
                {
                    *errno_tmp = ENOMEM;
                }
                return NULL;
            }

            /* new handler was successful -- try to allocate again */
        }
}

This post has been edited by mgrex: 28 September 2012 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#29 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Structure record binary file, refuses to display contents.

Posted 29 September 2012 - 05:50 AM

Yes. And if you look up the callstack, what call in your code is causing the problem?

You can see the callstack by going to Debug.Windows>Callstack.
Was This Post Helpful? 1
  • +
  • -

#30 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Re: Structure record binary file, refuses to display contents.

Posted 29 September 2012 - 06:15 AM

I have attached an image:

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3