Read Binary object into an array?

I know the topic may be hard to understand, but I am basically trying to take an already stored binary I/O file (which has 5 loan objects in it), read the results and store them into an array. This way I can add 5 to each byte.

Here's how I stored them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fstream loanf;// Create stream object
	loanf.open("Exercise13_6.dat", ios::out | ios::binary);

	// Instantiate loan objects
	Loan loan1(5.3, 10, 20000);
	Loan loan2(3.0, 5, 10000);
	Loan loan3(10.9, 7, 15000);
	Loan loan4(21.3, 5, 10000);
	Loan loan5(2.3, 3, 5000);

	// Write binary into file
	loanf.write(reinterpret_cast<char*> (&loan1), sizeof(Loan));
	loanf.write(reinterpret_cast<char*> (&loan2), sizeof(Loan));
	loanf.write(reinterpret_cast<char*> (&loan3), sizeof(Loan));
	loanf.write(reinterpret_cast<char*> (&loan4), sizeof(Loan));
	loanf.write(reinterpret_cast<char*> (&loan5), sizeof(Loan));

	loanf.close();


Here's how I read it..in a different cpp file: (this works)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Read array from the file
	fstream binaryio; // Create stream object

	// Read loan back from the file
	binaryio.open(inputFilename, ios::in | ios::binary);

	Loan newLoan;

	binaryio.seekg (0, ios::end);
	int length = binaryio.tellg() / sizeof(int);
	binaryio.seekg(0); //to return the file pointer to the beginning

	int *result = new int [length];

	// Display array
	for (int i = 0; i < length; i++)
	{
		// Reading one int at a time
		binaryio.read(reinterpret_cast<char*> (&newLoan), sizeof(Loan));
		if ( binaryio.eof() ) // Will check for end of file
			 break; // Exit from the while if end of file
		displayLoan(newLoan);
	}


I want to be able to do something like this though...(this code does not work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Read array from the file
	fstream binaryio; // Create stream object

	// Read loan back from the file
	binaryio.open(inputFilename, ios::in | ios::binary);


	binaryio.seekg (0, ios::end);
	int length = binaryio.tellg() / sizeof(int);
	binaryio.seekg(0); //to return the file pointer to the beginning
	Loan *newLoan;

	char *result = new char [length];

	// Display array
	for (int i = 0; i < length; i++)
	{
		// Reading one int at a time
		binaryio.read(reinterpret_cast<char*> (&newLoan[i]), sizeof(result[i]));
		if ( binaryio.eof() ) // Will check for end of file
			 break; // Exit from the while if end of file
		result[i] = newloan[i] + 5;
		// Then I will write result to another file
	}


Basically I want to retrieve the binary Loan objects that were stored in Exercise13_6.dat (which I can do). Then I want to store the objects in the result array.
Doesnt look like your allocating any memory for newLoan. For result[i] = newloan[i] + 5; to work you will likely need to overload operator+
How would I go about doing that for an object? I allocated memory for result...
I allocated memory for result...
Yes, but newLoan doesnt point anywhere.
What does your Loan object look like?
Here's the display from line 22 on the second piece of code:

1
2
3
4
5
6
void displayLoan(Loan &loan)
{
	cout << loan.getAnnualInterestRate() << " ";
	cout << loan.getNumberOfYears() << " ";
	cout << loan.getLoanAmount() << endl;
}
Something like this maybe,
1
2
3
4
5
6
7
Loan Loan::operator+(const int num) const
{
    Loan tmp = *this;
    //then add to the val you want
    tmp.loanAmount += num;
    return tmp;
}

You'll also want to overload operator= something like...
1
2
3
4
5
6
7
8
9
Loan& Loan::operator=(const Loan& op2)
{
     if(this != &op2)
     {
          Loan tmp;
          //copy vars
          tmp->var = op2->var;//etc
     }
}


Obviously I dont know your variable names, but this should get you in the right direction.
Topic archived. No new replies allowed.