avr39ripe

cppPlayerStructBinaryFileAdv

Jun 22nd, 2021
925
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. const int maxNameLen{ 50 };
  5.  
  6. struct Player
  7. {
  8.     int id;
  9.     char* name{ nullptr };
  10.     int rank;
  11.  
  12.     void init(int idP, const char* nameP, int rankP)
  13.     {
  14.         id = idP;
  15.  
  16.         setName(nameP);
  17.  
  18.         rank = rankP;
  19.     }
  20.  
  21.     void setName(const char* nameP)
  22.     {
  23.         auto len{ strlen(nameP) + 1 };
  24.  
  25.         if (!name)
  26.         {
  27.             name = new char[len];
  28.         }
  29.         else if (name and strlen(name) + 1 < len)
  30.         {
  31.             delete[] name;
  32.             name = new char[len];
  33.         }
  34.  
  35.         strcpy_s(name, len, nameP);
  36.     }
  37.  
  38.     void print()
  39.     {
  40.         std::cout << id << ' ' << name << ' ' << rank << '\n';
  41.     }
  42.  
  43.     void fileWrite(std::ostream& outf)
  44.     {
  45.         outf.write((char*)&id, sizeof(int));
  46.         auto nameLen{ strlen(name) + 1 };
  47.         outf.write(name, nameLen);
  48.         if (nameLen < maxNameLen) { outf.seekp(maxNameLen - nameLen, std::ios::cur); }
  49.         outf.write((char*)&rank, sizeof(int));
  50.     }
  51.  
  52.     void fileRead(std::ifstream& inf)
  53.     {
  54.         const int bufSize{ maxNameLen + 1 };
  55.         char buf[bufSize];
  56.  
  57.         inf.read((char*)&id, sizeof(int));
  58.         inf.read(buf, maxNameLen);
  59.         setName(buf);
  60.         //auto nameLen{strlen(buf)+1};
  61.         inf.read((char*)&rank, sizeof(int));
  62.     }
  63.  
  64.     bool changeName(const char* nameP)
  65.     {
  66.         std::fstream file;
  67.         int searchId{ 0 };
  68.         size_t nameLen{ 0 };
  69.         file.open("file.dat", std::ios::in | std::ios::out | std::ios::binary);
  70.  
  71.         while (!file.eof())
  72.         {
  73.             file.read((char*)&searchId, sizeof(int));
  74.             if (file.fail()) { break; }
  75.  
  76.             if (searchId == id)
  77.             {
  78.                 nameLen = strlen(nameP) + 1;
  79.                 file.seekp(file.tellg(), std::ios::beg);
  80.                 file.write(nameP, nameLen);
  81.                 file.close();
  82.  
  83.                 setName(nameP);
  84.  
  85.                 return true;
  86.             }
  87.             file.seekg(maxNameLen + sizeof(int), std::ios::cur);
  88.         }
  89.         file.close();
  90.         return false;
  91.     }
  92.  
  93.     void clear()
  94.     {
  95.         delete[] name;
  96.     }
  97. };
  98.  
  99. bool playerChangeName(Player& player, const char* nameP)
  100. {
  101.     std::fstream file;
  102.     int searchId{ 0 };
  103.     size_t nameLen{ 0 };
  104.     file.open("file.dat", std::ios::in | std::ios::out | std::ios::binary);
  105.  
  106.     while (!file.eof())
  107.     {
  108.         file.read((char*)&searchId, sizeof(int));
  109.         if (file.fail()) { break; }
  110.  
  111.         if (searchId == player.id)
  112.         {
  113.             nameLen = strlen(nameP) + 1;
  114.             file.seekp(file.tellg(), std::ios::beg);
  115.             file.write(nameP, nameLen);
  116.             file.close();
  117.  
  118.             player.setName(nameP);
  119.  
  120.             return true;
  121.         }
  122.         file.seekg(maxNameLen + sizeof(int), std::ios::cur);
  123.     }
  124.     file.close();
  125.     return false;
  126. }
  127.  
  128. void printPlayers(Player* begin, Player* end)
  129. {
  130.     for (auto player{ begin }; player != end; ++player)
  131.     {
  132.         player->print();
  133.     }
  134. }
  135.  
  136. void clearPlayers(Player* begin, Player* end)
  137. {
  138.     for (auto player{ begin }; player != end; ++player)
  139.     {
  140.         player->clear();
  141.     }
  142. }
  143.  
  144. void writePlayers(Player* begin, Player* end)
  145. {
  146.     std::ofstream outf;
  147.  
  148.     outf.open("file.dat", std::ios::binary);
  149.  
  150.     for (auto player{ begin }; player != end; ++player)
  151.     {
  152.         player->fileWrite(outf);
  153.     }
  154.  
  155.     outf.close();
  156. }
  157.  
  158. void readPlayers(Player* begin, Player* end)
  159. {
  160.     std::ifstream inf;
  161.  
  162.     inf.open("file.dat", std::ios::binary);
  163.  
  164.     for (auto player{ begin }; player != end; ++player)
  165.     {
  166.         player->fileRead(inf);
  167.     }
  168.  
  169.     inf.close();
  170. }
  171.  
  172. int main()
  173. {
  174.     std::cout << "Answer to question is..... : " << sizeof(Player) << '\n';
  175.  
  176.     const int playersCount{ 3 };
  177.     Player players[playersCount];
  178.     Player* const playersEnd{ players + playersCount };
  179.  
  180.     players[0].init(26007, "MadMax", 39);
  181.     players[1].init(28002, "Batman", 25);
  182.     players[1].setName("MishMans");
  183.     players[2].init(29002, "Joker", 15);
  184.  
  185.     printPlayers(players, playersEnd);
  186.     writePlayers(players, playersEnd);
  187.     //players[0].changeName("MaddYMaxxY");
  188.     playerChangeName(players[0], "SmallMax");
  189.  
  190.     std::cout << "\n\nReading players from file...\n\n";
  191.     Player filePlayers[playersCount];
  192.     Player* const filePlayersEnd{ filePlayers + playersCount };
  193.  
  194.     readPlayers(filePlayers, filePlayersEnd);
  195.     printPlayers(filePlayers, filePlayersEnd);
  196.  
  197.     clearPlayers(filePlayers, filePlayersEnd);
  198.     clearPlayers(players, playersEnd);
  199.  
  200. };
  201.  
RAW Paste Data