Jump to content
  • Advertisement
Sign in to follow this  
coderWalker

Writing and Reading files

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

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;
}
}

Share this post


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

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!