Writing and Reading files

Started by
1 comment, last by coderWalker 12 years, 9 months ago
My game has classes called chunks.
Each chunk has 2 16x16x16 unsigned char arrays called position and data.

I have two functions below a read and write.

Somehow between them the data becomes corrupt.
Is "(char)Chunk->position[j][k];" truncating my data?
The data array contains values from 0 to 255.

When it converts them from unsigned chars to chars is it losing the data? (even though thier converted back " newChunk->data[j][k] = (unsigned char)*pLoc;" )

After the readChunk routine runs I can look at the data[] and see that all of the values are 205.

Someone please point out my mistake.

Thanks,
Walker


bool file::writeChunk(chunk* Chunk)
{
if (Chunk->requiresWriting == false)
{
return false;
}

std::stringstream name;
std::string temp;

signed int x = Chunk->absolutePos.x;
signed int y = Chunk->absolutePos.y;
signed int z = Chunk->absolutePos.z;
ofstream fileStream;
////Open File to save in
name.str(std::string());
name << "save/" << world0->name << "x" << x << "x" << y << "x" << z << ".txt";
temp = name.str();
fileStream.open(temp.c_str(), ios::out|ios::binary);
if (fileStream.is_open())
{
////Make the 2 pointers
char *pStoreMemory;
char *pLoc;
////Allocate memory
pStoreMemory = new char [16*16*16*2];
pLoc = pStoreMemory;
////Write needed data to memory
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
*pLoc = (char)Chunk->position[j][k];
pLoc++;
}
}
}
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
*pLoc = (char)Chunk->data[j][k];
pLoc++;
}
}
}
////Copy memory to file
fileStream.write(pStoreMemory,16*16*16*2);
fileStream.close();
delete[] pStoreMemory;
//cout << "Done!\n";
}
else
{
//cout << "/!\\ Cannot open " << temp.c_str() << "\n";
}
delete Chunk;
return true;
}

chunk* file::readChunk(signed int x,signed int y,signed int z)
{
std::stringstream name;
std::string temp;

chunk* newChunk;
ifstream fileStreamRead;
////Open File to load from
name.str(std::string());
name << "save/" << world0->name << "x" << x << "x" << y << "x" << z << ".txt";
temp = name.str();
fileStreamRead.open(temp.c_str(), ios::in|ios::binary);
if (fileStreamRead.is_open())
{
iVertex3d position;
position.x = x;
position.y = y;
position.z = z;
newChunk = new chunk(position);
////Make the 2 pointers
char *pStoreMemory;
char *pLoc;
////Allocate memory
pStoreMemory = new char [16*16*16*2];
pLoc = pStoreMemory;
////Copy memory to file
fileStreamRead.read(pStoreMemory,16*16*16*2);
////read needed data from memory
//if ((unsigned char)*pLoc != 48)
{
//newChunk->empty = false;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
newChunk->position[j][k] = (unsigned char)*pLoc;

// /!\ Add to watch Queue if block has potential to move
switch (*pLoc)
{
case steam:
case smoke:
case water1:
case water2:
case water3:
case water4:
case water5:
case lava1:
case lava2:
case lava3:
case lava4:
case lava5:
case oil1:
case oil2:
case oil3:
case oil4:
case oil5:
case fire:
newChunk->liquidBufferAdd(i,j,k);
break;
}
pLoc++;
}
}
}
}
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
for (int k = 0; k < 16; k++)
{
newChunk->data[j][k] = (unsigned char)*pLoc;
}
}
}
fileStreamRead.close();
delete[] pStoreMemory;
return newChunk;
}
else
{
//cout << "/!\\ Cannot open " << temp.c_str() << "\n";
iVertex3d position;
position.x = x;
position.y = y;
position.z = z;
newChunk = new chunk(position);
generateChunk(newChunk);
return newChunk;
}
}
If this post was helpful please +1 or like it !

Webstrand
Advertisement
You are forgetting to increase pLoc in the second 3d loop in readChunk().

Of course, if position and data are 3d (unsigned) char arrays, why not just write (char *)position and (char *)data rather than copying them to another array?
Thanks a ton!!!

(I just knew it would be the unsigned char to char problem)

Your right! Works now!
If this post was helpful please +1 or like it !

Webstrand

This topic is closed to new replies.

Advertisement