Reading Binary Files

So I'm having problem with the output of this program. The program runs and compiles, except when I look at the output it gives me the default contructor for the seller array rather than reading in the data.
The current output is:
                                      0
                                      0
                                      0
                                      0
                                      0
                                      0
                                      0
                                      0
                                      0



The current code is:
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
#ifndef SELLER_H
#define SELLER_H

//*****************************************************************
// FILE:      Seller.h
// AUTHOR:    Shaikh Omar
// LOGON ID:  z1548925
// DUE DATE:  02/12/2012
//
// PURPOSE:   Contains the declaration for the Seller class.
//*****************************************************************

class Seller
      {
      private:
                char name[30];
                double salesTotal;

      public:
                Seller(); //default contructor
                Seller(char* newName, double newSalesTotal); //constructor for seller class

                const char* getName(); //returns seller's name
                double getSalesTotal(); //returns seller's sales total
                void setSalesTotal(double newSalesTotal); //set the seller's sales total
                void print(); //prints the saller's name and sales total

      };

#endif


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
#ifndef SALESDB_H
#define SALESDB_H
#include "Seller.h"

//*****************************************************************
// FILE:      SalesDB.h
// AUTHOR:    Shaikh Omar
// LOGON ID:  z1548925
// DUE DATE:  02/12/2012
//
// PURPOSE:   Contains the declaration for the SalesDB class.
//*****************************************************************

class SalesDB
      {
      private:
                Seller sellerArray[30]; //seller array
				int numSellers; //number of sellers

      public:
                SalesDB(); //default contructor
                SalesDB(const char* fileName); //constructor for salesDB class
                void print(); //prints the saller's name and sales total

      };

#endif


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
#include "Seller.h"
#include <iostream>
#include <iomanip>

using std::cout;
using std::string;
using namespace std;

//**********************************************************************************************
Seller::Seller() //default contructor
        {
        *name = '\0';
        salesTotal = 0;
        }

//**********************************************************************************************
Seller::Seller(char* newName, double newSalesTotal) //contructor that takes a character array containing a name and new sales total
        {
	strcpy (name, newName);	//copies the new name into the name array   
	salesTotal = newSalesTotal; //copies the new sales total 
        }

//**********************************************************************************************
const char* Seller::getName() //accessor that returns the name of the seller's name
        {
        return name;
        }

//**********************************************************************************************
double Seller::getSalesTotal() //accessor that returns the total sales of the seller
        {
        return salesTotal;
        }

//**********************************************************************************************
void Seller::setSalesTotal(double newSalesTotal) //accessor that takes the new sales total
        {
        salesTotal = newSalesTotal;
        }

//**********************************************************************************************
void Seller::print() //prints the seller's name and sales total
        { 
		
        cout <<left << setw(30) << name; 
		
	cout << right << setw (9) << salesTotal;
        }

//**********************************************************************************************


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
#include "SalesDB.h"
#include "Seller.h"
#include <iostream>
#include <iomanip>
#include <fstream>

using std::cout;
using std::string;
using namespace std;

SalesDB::SalesDB()
	{
	numSellers = 0;
	}
		
SalesDB::SalesDB (const char* fileName)
	{
	ifstream inFile;
	inFile.open (fileName, ios::binary);
	if (!inFile)
		{
		cout << "Error - unable to open input file " << fileName << endl;
		exit(1);
		}
	while (inFile)
		{
		inFile.read((char*) this, sizeof(SalesDB));
		}
	inFile.close();
	}
		
void SalesDB::print()
	{
	cout << "Sales Database Listing" << endl;
		
	for (int i = 0; i < numSellers; i++)
		{
		sellerArray[numSellers].print();
		}
	}
		
		


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "SalesDB.h"

using std::cout;
using std::endl;

int main ()
	{
	SalesDB region1("test.txt");
	region1.print();
		
	return 0;
	}
Last edited on
I'm having trouble reading your program because the indentation style you use is so absurd. Could you at least point out where you think the problem is?
I think the problem occurs the file is being read into the program and somewhere along there the data from the file isn't being filtered into where it should be needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SalesDB::SalesDB (const char* fileName)
	{
	ifstream inFile;
	inFile.open (fileName, ios::binary);
	if (!inFile)
		{
		cout << "Error - unable to open input file " << fileName << endl;
		exit(1);
		}
	while (inFile)
		{
		inFile.read((char*) this, sizeof(SalesDB));
		}
	inFile.close();
	}


I figure the problem might arising from section but not sure.
Shaikh Omar wrote:
inFile.read((char*) this, sizeof(SalesDB));
You know you can't do this, right? You need to manually read in each member.
I did not know. This is the first time I've dealt with a binary file. I was told that I could use that command to read in the file into my SalesDB object.
You can in some cases, but I really recommend against it.
Could you suggest any other method of writing the data in the binary file into an array? I've never really worked with binary files, only with regular text files.
You can read into the array in a similar manner, the problem is that you were using the this pointer instead of the array pointer.
Topic archived. No new replies allowed.