Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:confused:

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

dolgozo.zip

Posted

Nothing against your educational institution of choice, but they should have started you on C++ to begin with...and not classes/db programming.

 

Anyway, what's the text of the error it's throwing?

Posted

Well, I'll tell you what I think, see if that helps...

 

First, the compile errors:

 

dolgoszt d(file);

 

What is the d? I don't think that's valid syntax in c++.

Should just be: dolgoszt(file);

 

 

and this one:

 

adatok = new dolgozo[db];

 

 

Im pretty sure, this line: dolgozo *adatok;

won't allow you to save multiple dolgozo structures into it. A pointer is a container for a certain size of object and you are defining a pointer to a memory space for 1 dolgozo object. In this case, you might as well use an array, since you instantiate it as an array (adatok = new dolgozo[db]; ).

 

So, the change would be "dolgozo *adatok;" to "dolgozo adatok[];".

 

 

 

And for the logic errors:

 

1. The dolgoszt function doesn't give any output on success, so I would assume you want to call the kiir function after the call to it in the main function.

 

 

2. I think that this is a never ending loop:

 

while (!zh.eof())

{

db++;

}

 

 

unless eof increments the file pointer....which I don't think it does.

 

From what I see, you are trying to get the number of dolgozo structures in the file...You need to move the line before the while loop (zh.read((char*) &r, sizeof(dolgozo)); ) into the loop, so that the end condition will be met.

 

That does seem very inefficient though, you might want to refactor that part.

Posted

Code mistakes

 

First, the compile errors:

 

dolgoszt d(file);

 

What is the d? I don't think that's valid syntax in c++.

Should just be: dolgoszt(file);

 

The syntax is valid C++. dolgoszt is a class and d is an instance of dolgoszt, and the parameter file is passed to the constructor.

 

and this one:

 

adatok = new dolgozo[db];

 

 

Im pretty sure, this line: dolgozo *adatok;

won't allow you to save multiple dolgozo structures into it. A pointer is a container for a certain size of object and you are defining a pointer to a memory space for 1 dolgozo object. In this case, you might as well use an array, since you instantiate it as an array (adatok = new dolgozo[db]; ).

 

So, the change would be "dolgozo *adatok;" to "dolgozo adatok[];".

 

Again, incorrect. Martonx has the correct syntax for declaring and initializing an array - although you may wish to implement a destructor so that the array can be deleted when you are finished with the object.

 

1. The dolgoszt function doesn't give any output on success, so I would assume you want to call the kiir function after the call to it in the main function.

 

dolgoszt is a class, not a function.

 

The rest of your post, about the never ending loop, is accurate.

 

Good luck :cool:

Never trouble another for what you can do for yourself.
Posted

ios::beg

 

So, MrPaul, where is the compiler error then?

 

Most likely this line:

 

zh.beg;

 

I believe what is intended is:

 

zh.seekg(0, ios::beg);

 

Good luck :cool:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...