Hi again. I wrote 4 instances of a structure to a .dat file. I called them north, south, east, and west. I created a new program to read that file and display the data. I am using the below loop. I am trying to JUST read the north data. I keep getting only half the data using what i am doing below. What am i doing wrong? What should i be doing to read just one of the structures from the file.

Each structure for example has a Name, and the QTR #, and the sales figures for 4 quarters. I want to display the figures for all 4 qtrs for just the north in this case.

file1.read ( reinterpret_cast<char*>(&north), sizeof(north) );
	cout << "Info for the " << north.divName << " division: " << endl;
	while ( file1.read ( reinterpret_cast<char*>(&north), sizeof(north) ) )
	{
		cout << "Sales QTR " << north.qtr << ": " << north.salesPerQtr << endl;
		file1.read ( reinterpret_cast<char*>(&north), sizeof(north) );
	}

Any help or advice is greatly appreciated. Thanks again!

Recommended Answers

All 4 Replies

Dunno from the code you posted. Was the data even written properly in the first place? Did you open the fstream in binary mode?

Hmmm when you use sizeof(), I would sugguest using the structure name instead of the structure being loaded.
For example, if the structure was called sDirection:

file1.read ( reinterpret_cast<char*>(&north), sizeof(sDirection) );

I'm not to sure of the technicalities behind why that causes problems, but I know I've had the same issue before.

Dunno from the code you posted. Was the data even written properly in the first place? Did you open the fstream in binary mode?

i opened it in binary mode. Ah well i'll keep working on it. Should i post all the code?

i opened it in binary mode. Ah well i'll keep working on it. Should i post all the code?

Was my tip ignored completely? Or you tried it and it didn't work...

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.