Sign in to follow this  

custom binary file

This topic is 3044 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 [code]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

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

This topic is 3044 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this