Hi, i am trying to read from a random access file and display its contents to screen, however when i run the code nothing is displayed. I'm using a struct call "Menu" to store each record in the file and display it as the program moves through the file. Can anyone shed some light on this for me

thank you.

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

struct Menu
{

int idNum;
char foodItem[25];
float price;
char description[60];
};

void OpenInventory();
void main()
{
  OpenInventory();

}//end main

void OpenInventory()
{
	Menu m;
        ifstream infile;

        infile.open("Inventory.dat", ios::in | ios::binary);
	if(outfile.fail())
		{
			cout<<"Inventory.dat failed to open!"; exit(1);
		}

	while(infile >> m.idNum)
		{
			infile.getline(m.foodItem , 25, ',');
			infile >> m.price;
			infile.getline(m.description, 60, '.');

			infile.seekg(m.idNum * sizeof(Menu));
			infile.read(reinterpret_cast<char*>(&m), sizeof(Menu));

			cout<<m.idNum << m.foodItem << m.price << m.description <<endl;
		
		}
        infile.close();

}//end OpenInventory Function

I suspect that is reading the file incorrectly. If all the records in the file are of type struct menu then there is no need for 35 to 39. It just boils down to this:

while( infile.read(reinterpret_cast<char*>(&m), sizeof(Menu)) )
{
    cout<<m.idNum << m.foodItem << m.price << m.description <<endl;
}
commented: +1, since the OP didn't bother +17

Thanks that helped big time, the only problem is now that it prints empty records now.

Ancient Dragon, thanks a million. i figured out the rest, i was at this all day wondering why it wasnt displaying. again thank you!

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.