Sign in to follow this  
coderWalker

Writing and Reading files

Recommended Posts

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[i][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[i][j][k] = (unsigned char)*pLoc;" )

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

Someone please point out my mistake.

Thanks,
Walker

[code]
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[i][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[i][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[i][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[i][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;
}
}
[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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