Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems reading structures in VisualC++.NET

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
Hello again,

Has anyone come across general problems reading binary file data directly into structures, i.e.

fread ( &structure, sizeof(Structure), 1,inputResultsFile );

If I read each line individually
(using fread ( &int, sizeof(int),.... etc for multiple variables)
then it works, but as when I create a structure with all the correct variables and then try to read it then I get errors reading the values (its very inconsistent, it will read some of them ok, then it will read a corrupt value for no apparent reason??)

Any ideas/help/good luck messages appreciated!!!!

Cheers
 
1. Suppose you open file in binary mode (fopen(name,"rb")),
then
2.
Code:
struct S
{
  short x;
  double y;
  char c;
} s;

// sizeof(S) == 24 (!)
// not 11 = sizeof(s.x)+sizeof(s.y)+sizeof(s.c)!
Let's remember about word alignment, (garbage) padding bytes etc...
If we have wrote s member by member, we can't read it as whole and vice versa.
 
And if you want it to be size == 11, then you'll need to do this
Code:
#pragma pack(1)
struct S
{
  short x;
  double y;
  char c;
} s;
#pragma pack()
It depends on how the structure was packed when it was written out to the file in the first place. Writing structures in binary form is inherently non-portable.

At best, it can only fix your alignment problems. If the file comes from another machine, you could still have endian problems (and there's no magic to fix that).


--
 
Try the following code, i wrote it to work out if saving the data of a class directly to a file was quickly possible.

Code:
#include <stdio.h>
#include <conio.h>

void PressAnyKey();

class test
{
	public:
		test() { number = 10; number2 = 40; }
		int getnumber()
		{
			printf("%i\n", number);
			return number;
		}
		void setnumber(int newnumber)
		{
			int atesta = 0;
			number = newnumber;
		}

	private:
		int number;
		int number2;
};

typedef unsigned char byte;

int main()
{
	test a;
	a.setnumber(99);
	a.getnumber();

	FILE* pFile = NULL;
	pFile = fopen("C:\\memtest1.dat", "wb");
	fwrite((void *)&a, 1, sizeof(a), pFile);
	fclose(pFile);
	pFile = NULL;

	a.setnumber(10304);
	a.getnumber();

	pFile = fopen("C:\\memtest1.dat", "rb");
	fread((void *)&a, 1, sizeof(a), pFile);
	fclose(pFile);
	pFile = NULL;

	a.getnumber();

	test b;
	b.setnumber(99);
	b.getnumber();

	pFile = fopen("C:\\memtest2.dat", "wb");
	fwrite((void *)&b, 1, sizeof(b), pFile);
	fclose(pFile);
	pFile = NULL;

	b.setnumber(10304);
	b.getnumber();

	pFile = fopen("C:\\memtest2.dat", "rb");
	fread((void *)&b, 1, sizeof(b), pFile);
	fclose(pFile);
	pFile = NULL;

	b.getnumber();

	PressAnyKey();
	return 0;
}

void PressAnyKey()
{
	printf("Press any key to continue...\n");
	getch();
}

Hope that helps you out

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top