Jump to content
  • Advertisement
Sign in to follow this  
magoonr

Problems saving a 3D-Array

This topic is 2768 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

Hi im having problems with saving/loading a 3d array from a file. In some cases the values of the loaded datas dont fit the datas from the saved datas. Looks like some values are shifted.
Does C++ have a problem with "huge" multidimensional arrays?

This is the way i try to store/load the level:

void load_level(void) {
char filenamen[1024] = "maps/";
strcat (filenamen,editor_levelname);
FILE *stream;
stream = fopen(filenamen, "r" );
fread( level, sizeof( char ), 256*32*16*16, stream );
fclose( stream );
return;
}

void save_level(void) {

FILE *stream;
char filenamen[1024] = "maps/";
strcat (filenamen,editor_levelname);
stream = fopen(filenamen, "w+t" );
fwrite( level, sizeof( char ), 256*32*16*16, stream );
fclose( stream );
return;

}

Any help?
Thx :]

Share this post


Link to post
Share on other sites
Advertisement
You're writing in "w+t" (update text data) mode instead of "w" (write binary data) mode.

Share this post


Link to post
Share on other sites
You probably want to explicitly specify binary mode with "rb" and "wb". On most *nix systems "w" and "wb" and "wt" are all equivalent, but on some other systems "w" is actually "wt" not "wb".

Share this post


Link to post
Share on other sites
look how i load data

first i set the array lengths

then in loop i create a varialbe and load to it data then i assign var to table element

for (j = 0; j < pk; j++) {
tmatrixtype KK2;
fread(&KK2,sizeof(KK2),1,f);
Matrixarr[j] = KK2;

}





but first of all you must make sure that you save your 3d array correctly








void __fastcall TachoGLModel::LoadModel(AnsiString filename, bool ProjectTextureHandling)
{
if (FileExists(filename) == false) {
ShowMessage("File does not exists: INVALID MODEL FILENAME: "+filename);
return;
}

USE_COLOR_MAP = false;

int j,n;
int x;
int i;
int liczba;
textpoint * imo2;

FILE* f = fopen(filename.c_str(),"rb");
memset(&header,0,sizeof(TachoGLModelHeader));
fread(&header,sizeof(TachoGLModelHeader),1,f);

int m = header.LENGTH;

VBO_V.set_length(m);
VBO_N.set_length(m);
VBO_T.set_length(m);
//FACES_PICK_COLOR.set_length(10);
//FACES_PICK_COLOR[8][2] = 10.0f;
//VBO_CV=&VBO_V;
//
//VBO_CV->set_length(m);

//VBO_CN=&VBO_N;
//
//VBO_CN->set_length(m);

VBO_CT=&VBO_T;

VBO_CT->set_length(m);



for (j=0; j < m ; j++) {
t3dpoint imo;
fread(&imo,sizeof(imo),1,f);
VBO_V[j] = imo;
}



for (j=0; j < m ; j++) {
t3dpoint imo2;
fread(&imo2,sizeof(imo2),1,f);
VBO_N[j] = imo2;
}



for (j=0; j < m ; j++) {
textpoint imo3;
fread(&imo3,sizeof(imo3),1,f);
VBO_T[j] = imo3;
}






int pk;
fread(&pk,sizeof(pk),1,f); //ilosc scian


VBO_BE.set_length(pk);

Matrixarr.set_length(pk);

Textureindex.set_length(pk);


CMOK = pk;



for (j=0; j < CMOK ; j++) {
int CV;
fread(&CV,sizeof(CV),1,f);
Textureindex[j] = CV;


}

for (j = 0; j < pk; j++) {
tvbofacenfo KK;
fread(&KK,sizeof(KK),1,f);
VBO_BE[j] = KK;

}

for (j = 0; j < pk; j++) {
tmatrixtype KK2;
fread(&KK2,sizeof(KK2),1,f);
Matrixarr[j] = KK2;

}


Matrixarr.set_length(pk);
//for (i = 0; i < Textureindex.Length; i++) Textureindex = 0;
for (i = 0; i < Textures.Length; i++) Textures.ID = 0;


if (FileExists(filename+".texi") == true) {
AnsiString a;
TStringList * s = new TStringList();


if (ProjectTextureHandling == true) {
TStringList * d = new TStringList();
delete d;
}


s->LoadFromFile(filename+".texi");
Textures.set_length(s->Count);
AnsiString me = FCyfra->basedir;//+"OpenGL\\";
s->Text = StringReplace(s->Text,"%",me,TReplaceFlags() << rfReplaceAll);
for (i = 0; i < s->Count; i++) {
// int x = FOUND_IN_TEXTURE_COLLECTION(me+s->Strings);
int x = -1;
if (x < 0) {
//dodaj teksdture do ProjectTextures
}
else {
//jak juz jest to zmieniaj i tak textureindex
}

LoadTexture(me+s->Strings,Textures.pointer,false);
a = LowerCase(s->Strings);

Textures.ID = 0;



if (Pos("noclip",a) > 0) Textures.ID = 2;
if (Pos("nodraw",a) > 0) Textures.ID = 2;
if (Pos("caulk",a) > 0) Textures.ID = 2;


if (Pos("stripe_wht_dot",a) > 0) Textures.ID = 1;

//ShowMessage("HE : "+IntToStr(i));
//ShowMessage



if (Pos("tree",a) > 0) Textures.ID = 1;
//ShowMessage("ALPHA CHANNEL TEX");

}


delete s;

}



if (header.USEN == false)
if (header.USET == false)
{
// ShowMessage("Ich will ");
/* for (j=0; j < m ; j++) {
t3dpoint imoomo;
fread(&imoomo,sizeof(imoomo),1,f);
VBO_C[j] = imoomo;
}

*/
}



fclose(f);

// header.USEN=true;
// header.USET=true;

/*
for (i=0; i < VBO_V.Length; i++) {
VBO_V = (*VBO_C);

}*/



AnsiString colormapfile = ExtractFilePath(filename)+change_filename_ext(filename,"colormap");

if (FileExists( colormapfile ) == true ) {
LOAD_COLORMAP_FILE( colormapfile );
USE_COLOR_MAP = true;
}

//
//CreateCollArrays();
//
//CalcFaceDist();
//
//
//CalculateMinMax();
// CalcCenterPoint();


this->filename = filename;

ModelCanBeUsed = true;


}

[/quote]







do not be so stupid and save file like this

xlen ylen zlen

[x[[y][z]

for x




for y




for z

t3dpoint k;

blockread

table[x][y][z] = k;




it is the easiest way i think :) maybe not lol :]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!