The program is suppose to write and then read data from the binary file and use seekg to display specific data depending on what is entered by the user. It displays the correct information when 1 is entered but not 0. Any help would be greatly appreciated.

#include <iostream>
#include <fstream>
using namespace std;

class employee {
	char name[20];
	double salary;
public:
	employee (char *i, double s) { strcpy(name, i); salary = s;}
	friend ostream &operator<<(ostream &out_data, employee emp); //inserter
	void store (fstream &stream);
	void read_in (fstream &stream);
};

void employee::store(fstream &stream) {
		stream.write(name,20) ;
		stream.write((char *) &salary, sizeof(double)); }

void employee::read_in (fstream &stream) {     
		stream.read(name,20);
		name[20] = '\0';
		stream.read((char *) &salary, sizeof(double)); }

ostream &operator<<(ostream &stream, employee emp) {
	stream << emp.name << emp.salary;
	return stream; }

int main() {
	employee emp1("Nathan", 200.45);
		employee emp2("Zac", 100.45);
	employee temp(" ",0);

	fstream emp("G:/employee", ios::out | ios::binary);	
	emp1.store(emp);
	emp2.store(emp);
	emp.close();
	
	fstream in("G:/employee", ios::in | ios::binary);	
	int recNo;

	do{
    cout << "Which Record? "; 	cin >> recNo;
	in.seekg(recNo*20 + sizeof(double), ios::beg);
	temp.read_in(in);
	cout << temp;
	}while(in.good());

	in.close();
	return 0;
}

Recommended Answers

All 4 Replies

>recNo*20 + sizeof(double)
Presumably when recNo is 0, you want to seek to the 0th byte. But because the multiplication is done first, you get the (0 + sizeof(double))th byte.

I attempted to do the multiplication before that line and store it an a variable and got the same outcome.

It is also happening if I add another object, which is "emp3", writing to the file seems to be fine. It is just the seekg part that isn't working the way it is suppose to be.

>I attempted to do the multiplication before that line and store it an a variable and got the same outcome.
That's no surprise. These two pieces of code have the same incorrect math:

in.seekg(recNo*20 + sizeof(double), ios::beg);
int foo = recNo * 20;
in.seekg(foo + sizeof(double), ios::beg);

This is painfully obvious when you think about how the 0th record is at byte 0, but 0 + sizeof(double) is never going to be 0. You need to calculate the size of a record, then multiply it by the number of records.

>I attempted to do the multiplication before that line and store it an a variable and got the same outcome.
That's no surprise. These two pieces of code have the same incorrect math:

in.seekg(recNo*20 + sizeof(double), ios::beg);
int foo = recNo * 20;
in.seekg(foo + sizeof(double), ios::beg);

This is painfully obvious when you think about how the 0th record is at byte 0, but 0 + sizeof(double) is never going to be 0. You need to calculate the size of a record, then multiply it by the number of records.

I want to apologise for my denseness, I've been getting to grips with binary files. But I thank you for your help because I have got it working :)

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.