为什么当我写入文件时,结构在我的文件中消失了
Why do structs disappear in my file when i write to it
每当我将struct
写入二进制文件时,它消失后的任何struct
都会消失,尽管在它之前写入的任何struct
都会保留。
我正在二进制输出模式下打开文件流,我的struct
只包含原始数据类型。
我还确保为每个操作制作单独的文件流。
输出:
create players
765
51
save 1
765
save 2
765
51
** struct
定义**
struct player{
int UUID;
};
功能保存struct
//updates player information in the player database
bool savePlayer(player playerData){
//count how manny playrs are in file
// Create our objects.
fstream countstream;
int count = 0;
countstream.open ("player.bin", ios::binary | ios::in);
if(countstream.is_open()){
countstream.seekg(0, ios::end); //set position to end
count = countstream.tellg()/sizeof(player);
//retuns number of players in file by getting
//the index of the position and dividing it by the size of each player
//no loops required :D
}
countstream.close();
bool found = false;
//if file is not empty,look through it
if(count > 0){
player playerTable[count];
fstream readstream;
readstream.open ("player.bin", ios::binary | ios::in);
//build table
for(int i = 0; i < count; i++){
readstream.seekg(i, ios::beg); //set position to end
readstream.read(reinterpret_cast <char *> (&playerTable[i]),
sizeof(player));
readstream.close();
}
//check table
for(int i = 0; i < count; i++){
if(playerTable[i].UUID == playerData.UUID){
found = true;
playerTable[i] = playerData;
}
}
//write table back to file
if(found){
fstream writestream; //create writestream
writestream.open ("player.bin", ios::binary | ios::out);
for(int i = 0; i < count; i++){
writestream.seekg(i, ios::beg); //set position to player
writestream.write(reinterpret_cast <char *> (&playerTable[i]),
sizeof(player));
if(!writestream.fail()){
writestream.close();
return true;
}
else{
writestream.close();
return false;
}
readstream.close();
}
}
}
//append if not found
if(!found){
fstream appendstream;
appendstream.open ("player.bin", ios::binary | ios::out |
ios::app);
appendstream.write(reinterpret_cast <char *> (&playerData),
sizeof(player));
appendstream.close();
if(!appendstream.fail()){
appendstream.close();
return true;
}
else{
appendstream.close();
return false;
}
}
return false;
}
任何建议将不胜感激。
你让这变得比必要的困难得多。您不需要将所有数据读入数组。您可以将每个项目读入一个变量,并检查它是否是您要替换的项目。通过在读取和写入模式下打开文件,您可以在读取文件时简单地覆盖文件中的该条目。
bool savePlayer(player playerData){
player curPlayer;
fstream stream;
stream.open ("player.bin", ios::binary | ios::in | ios::out);
while(stream.read(reinterpret_cast<char*>(&curPlayer), sizeof(curPlayer))){
if (curPlayer.UUID == playerData.UUID) {
stream.seekg(-(sizeof curPlayer), ios::cur); // back up to location of current player
break;
}
}
stream.write(reinterpret_cast<char*>(&playerData), sizeof playerData);
stream.close();
return false;
}
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 调用 C 文件中的"C++"结构
- 结构包含在两个头文件中,这两个文件我都不拥有
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 介子复制/安装头文件到输出目录并保持文件夹结构
- Windows 将标准的 C 文件结构注入到正在运行的进程中
- 如何读取/查询文件系统和文件结构
- C++/Python 中对象和结构的二进制文件结构
- 在 C++ 和 C# 之间共享内存映射文件结构
- 在文件结构中使用字符串而不是带有 read() 和 write() 的字符数组
- C++ while 循环在自定义文件结构中
- QT C 读取自定义文件结构
- CPP 头文件结构错误
- 安卓rom文件结构和调试
- 文件映射和文件结构
- 文件结构,以避免数据损坏
- 文件处理中给出奇怪值的流(指向文件结构的指针)
- 程序c++程序的文件结构
- 常规c++程序头文件结构
- FreeBSD 和 Linux 中的 ELF 文件结构