Hi Everyone,

I have a C++ program lesson, but i can't to do it. I have a binary file with structured datas.
I have to read the datas, and take into a class (structure array).

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

struct dolgozo
	{
	char nev[30];
	unsigned int kor;
	long int fizetes;
	};

class dolgoszt
{
private: 
	dolgozo *adatok;
	int db;
public: 
	dolgoszt(char nev[])
	{
		int i=0;
		db=0;
		dolgozo r;
		ifstream zh;
		zh.open(nev);
		if (zh.fail()) {cout<<"Error when open the file";exit(1);}
		zh.read((char*) &r, sizeof(dolgozo));
		while (!zh.eof())						
		{
			db++;
		}
		adatok = new dolgozo[db];
		zh.beg;
		zh.read((char*) &r, sizeof(dolgozo));
		while (!zh.eof())						
		{
			adatok[i]=r;
			i++;
			zh.read((char*) &r, sizeof(dolgozo));
		}
		zh.close();
		}
	~dolgoszt();

	void kiir()
	{
		cout<<adatok->nev<<"\t"<<adatok->fizetes<<"\t"<<adatok->kor<<endl;
	}
	
};


int main()
{
	char file[30]="c:\\dolgozo.dat";
	dolgoszt d(file);
	cin.get();
}

But i can't compile this, i have some fatal error
I think this part has the problem :(

adatok = new dolgozo[db];
zh.beg;
zh.read((char*) &r, sizeof(dolgozo));
while (!zh.eof())						
{
	adatok[i]=r;
	i++;
	zh.read((char*) &r, sizeof(dolgozo));
}

I attached the binary file. Please help me. I am new in C++, i can programming in Visual Basic, but it is very hard to learn C++

I think your wrong :)

First of all: Thank you for using code tags!

You didn't give an implementation of the destructor, but you told the linker that it should be there somewhere: ~dolgoszt(); The easiest way is to change this: ~dolgoszt(); in this: ~dolgoszt(){}; , but you would have a memory leak.

In the constructor you say: adatok = new dolgozo[db]; , so you'll have to delete that memory. The destructor would look like this:

~dolgoszt()
{
    // code to delete adatok 
};

Thank you this issue was my shame :(

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.