Dislplaying Contents of a binary file

Hello,

I've written code that is supposed to write data contained in structures to a binary file, and then display the contents of the file by opening it, reading the data and outputting it onto the screen. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int NAME_SIZE = 6;

struct Company
{
	char name[NAME_SIZE];
	int quarter[4];
	double quarterSales[4];
	
};

//function prototypes
void setSales(Company *);

int main()
{
	char again;

	Company northDivision;
	char *northPtr = northDivision.name;
	northPtr = "North"; //Set name 

	Company southDivision;
	char *southPtr = southDivision.name;
	southPtr = "South";//Set name

	Company eastDivision;
	char *eastPtr = eastDivision.name;
	eastPtr = "East"; //Set name

	Company westDivision;
	char *westPtr = westDivision.name;
	westPtr = "West"; //Set name

	cout << "Program 12-11" << endl << endl;
	cout << "Enter the quarterly sales figures for the North Division" << endl << endl;
	setSales(&northDivision);//set sales for each quarter of the strutcure

	cout << endl << endl;

	cout << "Enter the quarterly sales figures for the East Division" << endl << endl;
	setSales(&eastDivision);

	cout << endl << endl;

	cout << "Enter the quarterly sales figures for the South Division" << endl << endl;
	setSales(&southDivision);

	cout << endl << endl;

	cout << "Enter the quarterly sales figures for the West Division" << endl << endl;
	setSales(&westDivision);

	cout << endl << endl;

	fstream File;
	
	cout << "Now writing data to CompanySales.dat" << endl;
	//write data to CompanySales.dat file
	File.open("CompanySales.dat", ios::out | ios::binary);
	File << setprecision(2) << fixed;
	//write north division data to file
	File.write(reinterpret_cast<char *>(&northDivision), sizeof(northDivision));
	//write east division data to file
	File.write(reinterpret_cast<char *>(&eastDivision), sizeof(eastDivision));
	//write south division data to file
	File.write(reinterpret_cast<char *>(&southDivision), sizeof(southDivision));
	//write west division data to file
	File.write(reinterpret_cast<char *>(&westDivision), sizeof(westDivision));
	File.clear();
	File.close();
	cout << "Writing complete." << endl << endl;

        //I'm not sure about my code from here on......

	cout << "Now Opening CompanySales.dat" << endl;
	File.open("CompanySales.dat", ios::in | ios::binary);

	Company division; // use for displaying file

	if (!File)
	{
		cout << "Error Opening file. program aborting.\n";
		return 0;
	}

	cout << setprecision(2) << fixed;
	cout << "Here are the sales figures for the companies divisions:" << endl << endl;
	//read the firs record
	File.read(reinterpret_cast<char *>(&division), sizeof(division));

	//while not at end of file, display records
	while(!File.eof());
	{
		//Display the record.
		cout << division.name << " Division" << endl;
		cout << "Sales for Quarter " << division.quarter[0] << ":";
		cout << "$" << division.quarterSales[0] << endl;
		cout << "Sales for Quarter " << division.quarter[1] << ":";
		cout << "$" << division.quarterSales[1] << endl;
		cout << "Sales for Quarter " << division.quarter[2] << ":";
		cout << "$" << division.quarterSales[2] << endl;
		cout << "Sales for Quarter " << division.quarter[3] << ":";
		cout << "$" << division.quarterSales[3] << endl;

		cout << endl << endl;
		cin.get(again);
		//read the next record
		File.read(reinterpret_cast<char *>(&division), sizeof(division));
	}

	cout << "That is all the data" << endl << endl;
	File.close();
	

	return 0;
}

void setSales(Company *c)
{
	int quartNum = 1;
	for (int count = 0; count < 4; count++)
	{
		c->quarter[count] = quartNum;
		cout << "Quarter " << quartNum++ << endl;
		do
		{
			cout << "(Must be a positive value)" << endl;
			cout << "$";
			cin >> c->quarterSales[count];
			cout << endl;
		}
		while(c->quarterSales[count] < 0.0);
	}
}


It looks like it works fine up to the point where I open the file again, the file exists and there is content in it, but when it gets to the while statement that is supposed to display the contents onto the screen, it just hangs.

Could anyone please enlighten this novice of programming?

Thanks in advance.
That's not how C string assignment works.
 
strcpy(northDivision.name,"North");
So instead of creating a pointer that points northDivision.name, I can simply use strcpy? I'd need to #include <string> correct?
<cstring>

I should also mention that strcpy() doesn't perform safety checks of any kind. If the destination buffer is too small to contain the source buffer, it'll just keep going:
1
2
3
char s[5];
strcpy(s,"Hell"); //OK
strcpy(s,"Hello"); //buffer overflow (the source is 5 characters long plus 1 null character) 
Ok, so I can simply use the strcpy function, all I have to do is make sure that the contents don't go into overflow.

Thanks for the tip helios
I have a problem displaying the contents of a file, and I need some input on it, I'll keep everything brief and well organized:

I'm creating records with structures, this is the structure definition:

1
2
3
4
5
6
7
8
9
const int NAME_SIZE = 6;

struct Company
{
	char name[NAME_SIZE];
	int quarter[4];
	double quarterSales[4];
	
};


and these are the created structures:
1
2
3
4
Company northDivision;
Company eastDivision;
Company southDivision;
Company westDivision;


I have populated these structures with data, and written that data to a file that is opened for output in binary mode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
File.open("CompanySales.dat", ios::out | ios::binary);
	//write north division data to file
	File.write(reinterpret_cast<char *>(&northDivision), sizeof(northDivision));

	//write east division data to file
	File.write(reinterpret_cast<char *>(&eastDivision), sizeof(eastDivision));

	//write south division data to file
	File.write(reinterpret_cast<char *>(&southDivision), sizeof(southDivision));

	//write west division data to file
	File.write(reinterpret_cast<char *>(&westDivision), sizeof(westDivision));

	File.close();


The file is created and there is data in it (about 224 bytes or so)
and then I open the file for input in binary mode as well as create a new Company structure to hold the data being read into memory from the file:
1
2
3
Company division; // use for displaying file

File.read(reinterpret_cast<char *>(&division), sizeof(division));


Now here is where things went wrong, when the program encounters this while loop, the program just hangs, it doesn't fail, it never even reaches the end of the file, it just sits there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	//while not at end of file, display records
	while(!File.eof());
	{
		//Display the record.
		cout << division.name << " Division" << endl;
		cout << "Sales for Quarter " << division.quarter[0] << ":";
		cout << "$" << division.quarterSales[0] << endl;
		cout << "Sales for Quarter " << division.quarter[1] << ":";
		cout << "$" << division.quarterSales[1] << endl;
		cout << "Sales for Quarter " << division.quarter[2] << ":";
		cout << "$" << division.quarterSales[2] << endl;
		cout << "Sales for Quarter " << division.quarter[3] << ":";
		cout << "$" << division.quarterSales[3] << endl;

		cout << endl << endl;
		//read the next record
		File.read(reinterpret_cast<char *>(&division), sizeof(division));
	}


All this code is built off of examples from my programming book and I can't seem to get any more info from it, what mistakes have I made? Any help would mean a world of thanks from me.

Thanks in advance for any help anyone can offer.
I don't think read() moves the file pointer. Add File.seekg(sizeof(division),std::ios::cur) just before the read() call.
I stuck that in, but I don't think that is what I'm looking for, I took out the while loop and just ran the read and display and it displayed the east division info, so I took out the file.seekg, and it displayed the north division info, which seems right, I think something is wrong with my while loop.
God I must be blind:
while(!File.eof()); //Error - semicolon - empty statement loop - infinite loop
Last edited on
This kind of stuff kills me, I gotta stop trusting my books so much, In my book, a part of some example code has it written as
while(!File.eof());
Damned typos.

Thanks guys, very much appreciated
There are two looping structures that use while.
1
2
3
while(expression){
  //code
}


and
1
2
3
do{
  //code
}while(expression);


Note that the first form does not use a semicolon, the second requires it.
One thing to add..

if its really a binary file, you are not going to use
while(!File.eof())

binary files are always read according to their structures and the size they have. filestream may think of data as eof character and will break the loop and leaving some data. Similarly, its not easy to recognize \n or 10 in binary.
eof() works differently with text and binary files.

As for the '\n' part, I have no idea what you're talking about.
Topic archived. No new replies allowed.