Advertisement
Stoycho_KK

Untitled

May 11th, 2021
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<vector>
  5.  
  6. const char FILE_NAME[] = "file.txt"; //kude se namira faila
  7.  
  8. class Entity {
  9.     std::string name;
  10.     size_t id;
  11.     std::string password;
  12. public:
  13.     Entity() { name = ""; id = 0; password = ""; }
  14.     Entity(const std::string& _name, size_t id, const std::string& _pass) { name = _name; this->id = id; password = _pass; }
  15.  
  16.     std::string getPass() const { return this->password; }
  17.  
  18.     std::string getName() const { return this->name; }
  19.  
  20.     void print() const { std::cout << this->name << " " << this->id << " " << this->password << '\n'; }
  21.  
  22.     size_t getID() const { return this->id; }
  23.  
  24.     ~Entity() { };
  25. };
  26.  
  27. void saveEntityToFile(const char* location, const Entity& toSave) {
  28.     std::ofstream outFile(location, std::ios::app);
  29.  
  30.     if (outFile.bad())
  31.         return;
  32.     //zapisvam: ime " " ID " " pass "\n"
  33.     outFile << toSave.getName() << " " << toSave.getID() << " " << toSave.getPass() << '\n';
  34.  
  35.     outFile.close();
  36. }
  37.  
  38. Entity readEntityFromFile(const char* info) {
  39.     char nName[1024]; //pole za novo ime
  40.     size_t ID = 0; //pole za ID
  41.     char nPass[1024]; //pole za pass
  42.  
  43.     int ind = 0; //index na info
  44.  
  45.     int nInd = 0; //index na name
  46.  
  47.     int pInd = 0; //index na pass
  48.  
  49.     while (info[ind] != ' ') //dokato ne stigna " " (poneje taka razdelqm dannite vuv faila na zapisvaneto) kopiram
  50.         nName[nInd++] = info[ind++];
  51.  
  52.     nName[nInd] = '\0';
  53.     ind++; //skipvam praznoto prostranstvo na koeto sum vmomenta i produljavam da cheta
  54.  
  55.     while (info[ind] != ' ') {
  56.         ID *= 10;
  57.         ID += info[ind++] - '0';
  58.     }
  59.     ind++;
  60.  
  61.     while (info[ind] != '\0')//\0 zashtoto e posledniq element i nqma " " a '\0'
  62.         nPass[pInd++] = info[ind++];
  63.     nPass[pInd] = '\0';
  64.  
  65.     return Entity(nName, ID, nPass); //vrushtam kopie ot zapisanoto entity
  66. }
  67.  
  68. int main() {
  69.     Entity test("Entity1", 1234, "Password");
  70.     saveEntityToFile(FILE_NAME, test);
  71.  
  72.  
  73.     Entity test2("Entity2", 5679, "Password2");
  74.     saveEntityToFile(FILE_NAME, test2);
  75.  
  76.  
  77.     Entity test3("Entity3", 1357, "Password3");
  78.     saveEntityToFile(FILE_NAME, test3);
  79.  
  80.     std::ifstream inFile(FILE_NAME); //otvarqm fail
  81.  
  82.     if (inFile.bad())
  83.         return 0;
  84.  
  85.     std::vector<Entity> data; //vektor kudeto shte pazq dannite
  86.  
  87.     while (!inFile.eof()) {
  88.         char buff[1024]; //char masiv v koito shte zapisvam redovete na faila(1 red = 1 entity)
  89.  
  90.         inFile.getline(buff, 1024);//getline vzema reda
  91.  
  92.         Entity readedEntity = readEntityFromFile(buff); //cheta
  93.        
  94.         if (strlen(buff) != 0)//poneje naktaq imam nov red koito pak go chete getlaina prosto kato ""
  95.             data.push_back(readedEntity); //push
  96.     }
  97.  
  98.     for (auto _val : data) //za vseki element vuv vektora izpulnqva koda po - dolu. dosta polezno e da se znae ako rabotish s std::vector
  99.         _val.print();
  100.  
  101.     inFile.close();
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement