Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
![]() |
C-/C++-Quelltext |
1 |
std::ofstream& write_binary(_T const& data, std::ofstream& out) |
![]() |
C-/C++-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifndef UTILITY_HPP__INCLUDED #define UTILITY_HPP__INCLUDED #pragma once namespace util { template <typename _T> std::ofstream& write_binary(_T const& data, std::ofstream& out) { return out.write(reinterpret_cast<const char*>(&data), sizeof (_T)), out; } template <> std::ofstream& write_binary(std::string const& data, std::ostream& out) { const std::size_t length(data.length() + 1); out.write(reinterpret_cast<const char*>(&size), sizeof (std::size_t)); out.write(data.c_str(), length); retern out; } template <typename _T> std::istream& read_binary(_T& data, std::istream& in) { return in.read(reinterpret_cast<char*>(&data), sizeof (_T)), in; } template <> std::istream& read_binary(std::string& data, std::istream& in) { std::size_t length(0); in.read(reinterpret_cast<char*>(length), sizeof (std::size_t)); char* temp(new char[length]); in.read(temp, length); data = temp; delete [] temp; return in; } } // util #endif // UTILITY_HPP__INCLUDED |
![]() |
C-/C++-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#ifndef SCORE_HPP__INCLUDED #define SCORE_HPP__INCLUDED #pragma once struct Score { Score(std::string const& name = "", const long points = 0, std::string const& date = "") : name(name), points(points), date(date) {} std::string name; long points; std::string date; }; #endif // SCORE_HPP__INCLUDED |
![]() |
C-/C++-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <vector> #include <fstream> #include "Utility.hpp" #include "Score.hpp" void Highscore::load() { std::ifstream file_stream("Highscore.bin", std::ios_base::binary); if (!file_stream) throw std::runtime_exception("no resources access"); while (file_stream) { Score temp; util::read_binary(temp.name, in); util::read_binary(temp.points, in); util::read_binary(temp.date, in); m_scores.push_back(temp); } } void Highscore::save() const { std::ofstream file_stream("Highscore.bin", std::ios_base::binary); if (!file_stream) throw std::runtime_exception("no resources access"); for (std::vector<std::string>::const_iterator it(scores.begin()), end(scores.end()); it != end; ++it) { util::write_binary(it->name, file_stream); util::write_binary(it->points, file_stream); util::write_binary(it->date, file_stream); } } |
![]() |
C-/C++-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
void Highscores::loadHighscores() { struct stat stFileInfo; if(0==stat("Highscores.bin",&stFileInfo)) { // Datei auslesen ifstream data ("Highscores.bin", ios::in | ios::binary); data.seekg (0, ios::end); int length = data.tellg(); data.seekg (0, ios::beg); char* text = new char[length]; data.read(text,length); data.close(); // Daten verarbeiten string sToken; vector<string> tokens; for(int i = 0;i<length;i++) { if(text[i]=='\n') { tokens.push_back(sToken); sToken = ""; } else { sToken = sToken + text[i]; } } int profiles = atoi(tokens[0].c_str()); int token = 1; long points; string name,date; Score s; for(int i = 0;i<profiles;i++) { name = tokens[token]; token++; points = atoi(tokens[token].c_str()); token++; date = tokens[token]; token++; s.name = name; s.points = points; s.date = date; scores.push_back(s); } delete[] text; } } void Highscores::saveHighscores() { ofstream data ("Highscores.bin", ios::out | ios::binary); int scoresSize = scores.size(); save(data,toString(scoresSize)+"\n"); // Anzahl der Profile speichern for(int i = 0;i<scoresSize;i++) { save(data,scores[i].name+"\n"); // Name speichern save(data,toString(scores[i].points)+"\n"); // Punkte speichern save(data,scores[i].date+"\n"); // Datum speichern } data.close(); } void Highscores::save(std::ofstream &data, std::string content) { data.write(content.c_str(),content.size()*sizeof(char)); } |
Mein Programm funktioniert super, nur dass er nicht binär schreibt, obwohl ich binary flags verwende, das verwundert mich wirklich
![]() |
C-/C++-Quelltext |
1 2 |
int scoresSize = scores.size(); data.write( scoreSize, sizeof(int) ); |
Werbeanzeige