簡單解釋一下:使用<fstream>,我將一個std::list實體寫入.txt檔案:
#include <fstream>
#include <list>
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
std::ofstream file;
file.open("record.txt", std::ios::trunc);
if (file.is_open()) {
file.write((char*)&list_1, sizeof(std::list<Item>)) << std::endl;
file.close();
}
但是,當我從同一個檔案中讀取資料并將資料分配給一個std::list實體時:
file.open("record.txt", std::ios::in);
if (file.is_open()) {
std::list<Item> list_1;
file.read((char*)&list_1, sizeof(std::list<Item>));
}
當我嘗試訪問它的元素時,它給了我一個錯誤。然而,這不是我的問題。因為std::list存盤指向該元素的指標,所以我必須手動存盤元素,就像我在這里所做的那樣:
for (auto const& item : list_1) {
file << item.amount << std::endl;
file << item.value << std::endl;
file << item.item_name << std::endl;
file << (char*)&item.type << std::endl;
}
然后我讀了這些值。使用這些值創建一個新Item實體并將其存盤在 my list. 旁注:我可以從檔案中訪問size()屬性,因為它是堆疊中的成員。所以它被.list_1.txtstd::list<Item>ofstream
for (int i = 0; i < list_1.size(); i ) {
int amount = 0;
int value = 0;
std::string item_name;
Item_Type type = item;
file >> amount;
file >> value;
file >> item_name;
file >> (char*)&type;
Item item(amount, value, item_name, type);
main_player_inv.push_back(item);
我希望這可以作業,因為現在std::list應該沒有未初始化的成員,對吧?
好吧,它給了我這個錯誤:
this->_Mypair._Myval2._Myhead 是 0x228B4050240
這基本上意味著list_1->_Mypair._Myval2._Myhead是一個指向記憶體越界的指標。問題是,與我可以手動保存值和初始化的元素指標不同,我無法手動訪問list_1->_Mypair._Myval2._Myhead或編輯它的資料,因為它是一個私有成員。或者,我沒有辦法在網上找到。
所以,我有兩個問題:
我可以初始化
list_1->_Mypair._Myval2._Myhead以使其指向有效的記憶體嗎?有沒有一種方法可以更輕松地序列化 a
std::list并檢索它的內容?
如果這兩個問題都無法回答,我想談談我正在嘗試做的事情:
std::list<Item>用作角色或物品的物品欄。在我的專案中,我想將player容器等物件的專案存盤在一個std::list<Item>實體中。我認為這對于面向物件的 Player 結構來說是最合適的做法。以下是一些類,例如:
玩家等級
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
};
物品類別
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
};
如果您知道存盤玩家物品的更好方法,物品就是類實體;或者完全知道一個更好的方法來做到這一點,請幫助我。
uj5u.com熱心網友回復:
您無法讀取/寫入std::list物件(或任何其他重要型別)的原始位元組,因為您將寫入/讀取您不需要關心的原始指標和其他內部資料成員。
正如您已經發現的那樣,您必須(反)序列化您班級的各個資料成員。
我建議使用二進制格式而不是文本格式,例如:
#include <type_traits>
template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
void writeToStream(std::ostream &out, const T &value) {
out.write(reinterpret_cast<const char*>(&value), sizeof(value));
}
template <typename T, std::enable_if_t<std::is_scalar<T>::value, bool> = true>
void readFromStream(std::istream &in, T &value) {
in.read(reinterpret_cast<char*>(&value), sizeof(value));
}
void writeToStream(std::ostream &out, const std::string &value) {
size_t size = value.size();
writeToStream(out, size);
out.write(value.c_str(), size);
}
void readFromStream(std::istream &in, std::string &value) {
size_t size;
readFromStream(in, size);
value.resize(size);
in.read(value.data() /* or: &value[0] */, size);
}
template <typename Container>
void writeToStream(std::ostream &out, const Container &items) {
size_t count = items.size();
writeToStream(out, count);
for(const auto& item : items) {
writeToStream(out, item);
}
}
template <typename Container>
void readFromStream(std::istream &in, Container &items) {
size_t count;
readFromStream(in, count);
items.reserve(count);
for(size_t i = 0; i < count; i) {
Container::value_type item;
readFromStream(in, item);
items.push_back(item);
}
}
template<typename Container>
void writeToFile(const std::string &fileName, const Container &items) {
std::ofstream file(fileName, std::ios::binary);
file.exceptions(std::ofstream::failbit);
writeToStream(file, items);
}
template<typename Container>
void readFromFile(const std::string &fileName, Container &items) {
std::ifstream file(fileName, std::ios::binary);
file.exceptions(std::ifstream::failbit);
readFromStream(file, items);
}
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
void writeTo(std::ostream &out) const {
writeToStream(out, amount);
writeToStream(out, value);
writeToStream(out, item_name);
writeToStream(out, type);
}
void readFrom(std::istream &in) {
readFromStream(in, amount);
readFromStream(in, value);
readFromStream(in, item_name);
readFromStream(in, type);
}
};
void writeToStream(std::ostream &out, const Item &item) {
item.writeTo(out);
}
void readFromStream(std::istream &in, Item &item) {
item.readFrom(in);
}
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
void writeTo(std::ostream &out) const {
writeToStream(out, level);
writeToStream(out, experience);
writeToStream(out, health);
writeToStream(out, resistance);
writeToStream(out, inventory);
}
void readFrom(std::istream &in) {
readFromStream(in, level);
readFromStream(in, experience);
readFromStream(in, health);
readFromStream(in, resistance);
readFromStream(in, inventory);
}
};
void writeToStream(std::ostream &out, const Player &player) {
player.writeTo(out);
}
void readFromStream(std::istream &in, Player &player) {
player.readFrom(in);
}
#include <fstream>
#include <list>
int main() {
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
writeToFile("record.txt", list_1);
list_1.clear();
readFromFile("record.txt", list_1);
return 0;
}
如果您真的想要一個文本檔案,請使用operator<<并operator>>在您的類中覆寫它們,例如:
(隨意調整它以使用您想要的任何格式......)
#include <limits>
void discardLine(std::istream &in) {
in.ignore(std::numeeric_limits<std::streamsize>::max(), '\n');
}
template<typename CharT, typename Traits>
void streamFailed(std::basic_ios<CharT,Traits> &stream) {
stream.setstate(std::ios_base::failbit);
}
template <typename Container>
std::ostream& operator<<(std::ostream &out, const Container &items) {
out << '[' << items.size() << '\n';
for(const auto& item : items) {
out << item << '\n';
}
out << ']\n';
return out;
}
template <typename Container>
std::istream& operator>>(std::istream &in, Container &items) {
char ch;
in >> ch;
if (ch != '[') {
streamFailed(in);
} else {
size_t count;
in >> count;
discardLine(in);
items.reserve(count);
for(size_t i = 0; i < count; i) {
Container::value_type item;
in >> item;
items.push_back(item);
}
in >> ch;
if (ch != '[') {
streamFailed(in);
}
}
}
template<typename Container>
void writeToFile(const std::string &fileName, const Container &items) {
std::ofstream file(fileName, std::ios::binary);
file.exceptions(std::ofstream::failbit);
file << items;
}
template<typename Container>
void readFromFile(const std::string &fileName, Container &items) {
std::ifstream file(fileName, std::ios::binary);
file.exceptions(std::ifstream::failbit);
file >> items;
}
struct Item {
public:
unsigned int amount, value;
std::string item_name;
Item_Type type; // enum
public:
Item() :
amount(0), value(0), item_name("undefined"), type(item) {};
Item(int amount, int value, std::string item_name, Item_Type type) :
amount(amount), value(value), item_name(item_name), type(type) {};
friend std::ostream& operator<<(std::ostream &out, const Item &item) {
out << '(' << item.amount << ' ' << item.value << ' ' << static_cast<int>(item.type) << ' ' << item.item_name << ')';
return out;
}
friend std::istream& operator>>(std::istream &in, Item &item) {
char ch;
in >> ch;
if (ch != '(') {
streamFailed(in);
} else {
int itype;
in >> item.amount >> item.value >> itype;
item.type = static_cast<Item_Type>(itype);
std::getline(in >> std::ws, item_name, ')');
}
return in;
}
};
class Player : public Object {
public:
int level, experience;
double health;
float resistance; // 0.000 - 1.000
std::list<Item> inventory;
public:
Player() :
level(0), experience(0), health(10.0), resistance(0.0f) {};
Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
friend std::ostream& operator<<(std::ostream &out, const Player &player) {
out << '(' << level << ' ' << experience << ' ' health << ' ' << resistance << '\n';
out << inventory;
out << ')';
return out;
}
friend std::istream& operator>>(std::istream &in, Player &player) {
char ch;
in >> ch;
if (ch != '(') {
streamFailed(in);
} else {
in >> player.level >> player.experience >> player.health >> player.resistance >> player.inventory;
in >> ch;
if (ch != ')') {
streamFailed(in);
}
}
return in;
}
};
#include <fstream>
#include <list>
int main() {
std::list<Item> list_1; //example list
list_1.push_back(Item(...));
writeToFile("record.txt", list_1);
list_1.clear();
readFromFile("record.txt", list_1);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513365.html
標籤:C c 11流标准列表
