使用 fstream 写入类型向量
using fstream to write vector of types
我正在尝试编写我的 chunks 向量,其中包含 Tiles 向量。写入正确的值似乎有些不对劲。虽然它应该有 25000 个 Tile 对象,但文件的大小是 4 个字节...
world.cpp
Tile tile;
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
tile.setCoord(sf::Vector2i(x, y));
tile.setBiome(0);
chunk.push_back(tile);
}
}
world.push_back(chunk);
ofstream file("saves/map.dat", ios::binary | ios::out);
size_t s = world.size() * (chunk.size() * sizeof(Tile));
file.write((char *) &world, sizeof(s));
file.close();
world.hpp
class World {
public:
// World getters
int getTileSize() { return tileSize; };
int getWorldSize() { return (width * tileSize) * (height * tileSize); };
void load(const sf::String& filename);
void save(const sf::String& filename);
void draw(sf::RenderWindow& window, float dt);
World(sf::Clock clock); // new
World(const sf::String& filename, sf::Clock clock); // load
~World();
private:
const int chunkSize = 64;
int tileSize = 32; //default 32
int width, height;
std::vector<Tile> chunk;
std::vector<std::vector<Tile>> world;
sf::Clock clock;
};
如果需要,这里是磁贴 class:
class Tile {
public:
void draw(sf::RenderTarget& target, sf::RenderStates states);
Tile();
Tile(sf::Vector2i coord, int biome);
~Tile();
sf::Vector2i getCoord() { return coord; };
int getBiome() { return biome; };
void setCoord(sf::Vector2i coord) { this->coord = coord; };
void setBiome(int biome) { this->biome = biome; };
private:
sf::Vector2i coord;
int biome;
};
我检查过写入的变量确实填充了所有对象。所以问题出在写作过程中,但我不知道哪里......
file.write((char *) &world, sizeof(s));
这行不通,有几个原因。
首先,sizeof(s)
通常只有 4 或 8 个字节(取决于您的编译器和您的目标位数)。我假设你的意思是 s
.
即使您使用 s
而不是 sizeof(s)
作为要写入数据的长度,sizeof(std::vector)
通常也只有 12 或 24 个字节(大小、容量和指针到数据)。根本没有 s
字节可写。
您的数据不是平面结构,因此您无法一次将其全部写入。研究序列化。有许多库可以帮助解决这个问题,例如 boost serialization or google protobuf,但您不一定非要使用某个库。您只需要想出一个可用于写入数据并保留足够信息以在以后重新创建数据结构的结构。
我正在尝试编写我的 chunks 向量,其中包含 Tiles 向量。写入正确的值似乎有些不对劲。虽然它应该有 25000 个 Tile 对象,但文件的大小是 4 个字节...
world.cpp
Tile tile;
for (int x = 0; x < 500; x++)
{
for (int y = 0; y < 500; y++)
{
tile.setCoord(sf::Vector2i(x, y));
tile.setBiome(0);
chunk.push_back(tile);
}
}
world.push_back(chunk);
ofstream file("saves/map.dat", ios::binary | ios::out);
size_t s = world.size() * (chunk.size() * sizeof(Tile));
file.write((char *) &world, sizeof(s));
file.close();
world.hpp
class World {
public:
// World getters
int getTileSize() { return tileSize; };
int getWorldSize() { return (width * tileSize) * (height * tileSize); };
void load(const sf::String& filename);
void save(const sf::String& filename);
void draw(sf::RenderWindow& window, float dt);
World(sf::Clock clock); // new
World(const sf::String& filename, sf::Clock clock); // load
~World();
private:
const int chunkSize = 64;
int tileSize = 32; //default 32
int width, height;
std::vector<Tile> chunk;
std::vector<std::vector<Tile>> world;
sf::Clock clock;
};
如果需要,这里是磁贴 class:
class Tile {
public:
void draw(sf::RenderTarget& target, sf::RenderStates states);
Tile();
Tile(sf::Vector2i coord, int biome);
~Tile();
sf::Vector2i getCoord() { return coord; };
int getBiome() { return biome; };
void setCoord(sf::Vector2i coord) { this->coord = coord; };
void setBiome(int biome) { this->biome = biome; };
private:
sf::Vector2i coord;
int biome;
};
我检查过写入的变量确实填充了所有对象。所以问题出在写作过程中,但我不知道哪里......
file.write((char *) &world, sizeof(s));
这行不通,有几个原因。
首先,sizeof(s)
通常只有 4 或 8 个字节(取决于您的编译器和您的目标位数)。我假设你的意思是 s
.
即使您使用 s
而不是 sizeof(s)
作为要写入数据的长度,sizeof(std::vector)
通常也只有 12 或 24 个字节(大小、容量和指针到数据)。根本没有 s
字节可写。
您的数据不是平面结构,因此您无法一次将其全部写入。研究序列化。有许多库可以帮助解决这个问题,例如 boost serialization or google protobuf,但您不一定非要使用某个库。您只需要想出一个可用于写入数据并保留足够信息以在以后重新创建数据结构的结构。