0

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!

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by skatamatic
0

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

0

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.

0

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?

0

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...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.