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++

Dani AI

Generated

A few concrete points that will fix the crash and make the code robust.

  • Primary problems seen in the original post: the file was not opened in binary mode, the loop used the eof pattern incorrectly (the constructor only read once while counting), zh.beg is not a valid way to rewind the stream, and a destructor was declared but not implemented (linker error). As noted, manual new[]/delete[] invites leaks — prefer RAII.
  • A safe, simple pattern: open with ios::binary, use seekg/tellg to get file size, compute record count as filesize / sizeof(dolgozo) (and check for a remainder), then read all records at once into a std::vector<dolgozo>. That avoids manual counting loops and reset bugs, and it is fast.

Example reading function (uses RAII via std::vector):

std::vector<dolgozo> read_all(const char* filename)
{
    std::ifstream in(filename, std::ios::binary);
    if (!in) throw std::runtime_error("cannot open file");

    in.seekg(0, std::ios::end);
    std::streampos bytes = in.tellg();
    if (bytes <= 0) return {};

    if (bytes % sizeof(dolgozo) != 0) throw std::runtime_error("file size not multiple of record");
    size_t count = static_cast<size_t>(bytes / sizeof(dolgozo));

    std::vector<dolgozo> v(count);
    in.seekg(0, std::ios::beg);
    in.read(reinterpret_cast<char*>(v.data()), count * sizeof(dolgozo));
    if (!in) throw std::runtime_error("read failed");

    return v;
}

Additional practical notes: ensure the in-file layout matches your struct (packing, field sizes and endianness). char nev[30] may not be NUL-terminated, so convert it to std::string safely (scan up to 30 bytes for a NUL). If you must use raw new[], implement ~dolgoszt() to delete[] the array, but std::vector removes that boilerplate. See std::vector and std::istream::read for details.

Recommended Answers

All 2 Replies

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.