Jump to content
  • Advertisement
Sign in to follow this  
assainator

custom binary file

This topic is 3258 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lately i was trying to create a custom binary file format. I started simple, and added more and more. At the moment I'm having a file format that stores all the vertexs in a mesh. but once i try to load it I'm getting a problem:
[source="cpp"]
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
	int Major;
	int Minor;
	int revision;
	int vertexCount;
}M2FFILEHEADER;

typedef struct
{
	float x,y,z;
}VERTEX;

int main()
{

        M2FFILEHEADER readHeader;
			
	FILE *binM2fStream = fopen("test.bin", "rb");
	fread(&readHeader, sizeof(M2FFILEHEADER), 1, binM2fStream);

	VERTEX vertex[readHeader.vertexCount];//this is the problem line

	for(int readVert = 0; readVert < readHeader.vertexCount; readVert++)
	{
		fseek(binM2fStream, (sizeof(M2FFILEHEADER)+sizeof(VERTEX)*readVert), SEEK_SET);

		fread(&vertex[readVert], sizeof(VERTEX), 1, binM2fStream);
		printf("Vertex #%i\n  X: %f\n  Y: %f\n  Z: %f\n", readVert, vertex[readVert].x, vertex[readVert].y, vertex[readVert].z);
	}
}



As you can see it prints the geometry when loading. But once i compile it, i get these errors:
Quote:
binaryreader\main.cpp(20) : error C2057: expected constant expression binaryreader\main.cpp(20) : error C2466: cannot allocate an array of constant size 0 binaryreader\main.cpp(20) : error C2133: 'vertex' : unknown size
if i change
VERTEX vertex[readHeader.vertexCount];
to
VERTEX vertex[8];
it works fine, but if i change it to readHeader.vertexCount = 8; VERTEX vertex[readHeader.vertexCount]; it still crashes thanks in advance for any help you people can give me, assainator

Share this post


Link to post
Share on other sites
Advertisement

VERTEX* buf = new VERTEX[readHeader.vertexCount];

for(int readVert = 0; readVert < readHeader.vertexCount; readVert++)
{
fseek(binM2fStream, (sizeof(M2FFILEHEADER)+sizeof(VERTEX)*readVert), SEEK_SET);

fread(&buf[readVert], sizeof(VERTEX), 1, binM2fStream);
printf("Vertex #%i\n X: %f\n Y: %f\n Z: %f\n", readVert, buf[readVert].x, buf[readVert].y, buf[readVert].z);
}

delete[] buf;



C2057.

Share this post


Link to post
Share on other sites
EDIT: Oops should have read all the way through the post first. You need to allocate the array dynamically. Arrays created on the stack have to be created with a constant size. The post above has it right.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!