Untitled
unknown
plain_text
2 years ago
1.5 kB
9
Indexable
#include <iostream>
#include <fstream>
#include <string>
struct Player {
std::string name;
std::string password;
int gems;
};
void writePlayerToFile(const std::string& fileName, const Player& player) {
std::ofstream file(fileName, std::ios::binary);
if (file.is_open()) {
// Struct'ı dosyaya yaz
file.write(reinterpret_cast<const char*>(&player), sizeof(Player));
file.close();
std::cout << "Oyuncu bilgileri binary olarak kaydedildi." << std::endl;
} else {
std::cerr << "Dosya acma hatasi!" << std::endl;
}
}
void readPlayerFromFile(const std::string& fileName, Player& player) {
std::ifstream file(fileName, std::ios::binary);
if (file.is_open()) {
// Struct'ı dosyadan oku
file.read(reinterpret_cast<char*>(&player), sizeof(Player));
file.close();
std::cout << "Oyuncu Ismi: " << player.name << std::endl;
std::cout << "Oyuncu Sifresi: " << player.password << std::endl;
std::cout << "Oyuncu Gems: " << player.gems << std::endl;
} else {
std::cerr << "Dosya acma hatasi!" << std::endl;
}
}
int main() {
// Oyuncu bilgilerini oluştur
Player player;
player.name = "Oyuncu1";
player.password = "sifre123";
player.gems = 100;
// Veriyi dosyaya yazma
writePlayerToFile("oyuncu_bilgileri.dat", player);
// Dosyadan veriyi okuma
Player readPlayer;
readPlayerFromFile("oyuncu_bilgileri.dat", readPlayer);
return 0;
}
Editor is loading...
Leave a Comment