Advertisement
alexx876

Untitled

Oct 31st, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include "file.h"
  2.  
  3. //Create new file
  4. ofstream new_DB(char file[50]) {
  5.     ofstream ofile(file);
  6.     return ofile;
  7. }
  8. //Remove file
  9. void remove_DB(char file[50]) {
  10.     ifstream fin(file);
  11.     if (fin.is_open()) {
  12.         fin.close();
  13.         if (remove(file)) {
  14.             cout << "Error removing file\n";
  15.             return;
  16.         }
  17.         cout << "Успех! Файл базы успешно удален.\n";
  18.     }
  19. }
  20.  
  21. void save_DB(playlist *albums, int count, char file[50]) {
  22.     ofstream four = new_DB(file);
  23.     four << count << endl;
  24.     char temp[101]; int last = 0;
  25.     for (int i = 0; i < count; i++) {
  26.  
  27.         for (int k = 0; albums[i].title[k] != 0; k++) {
  28.             if (albums[i].title[k] != ' ') temp[k] = albums[i].title[k];
  29.             else temp[k] = '^';
  30.             last = k;
  31.         }
  32.         temp[last + 1] = '\0';
  33.  
  34.         four << temp << endl;
  35.         for (int k = 0; albums[i].artist[k] != 0; k++) {
  36.             if (albums[i].artist[k] != ' ') temp[k] = albums[i].artist[k];
  37.             else temp[k] = '^';
  38.             last = k;
  39.         }
  40.         temp[last + 1] = '\0';
  41.  
  42.         four << temp << endl;
  43.         four << albums[i].year << endl;
  44.         four << albums[i].tracks << endl;
  45.         four << albums[i].minutes << endl;
  46.         four << albums[i].singles << endl;
  47.         four << albums[i].sells << endl;
  48.         four << albums[i].mark << endl;
  49.     }
  50.     four.close();
  51.     cout << "Запись в файл прошла успешно!";
  52. }
  53. void load_DB(playlist *albums, char file[50], int count) {
  54.     ifstream fin(file);
  55.     if (!fin.is_open()) {
  56.         new_DB(file);
  57.     } else {
  58.         fin >> count;
  59.         char temp[101]; int last = 0;
  60.         if (fin) {
  61.             for (int i = 0; i < count; i++) {
  62.                 fin >> temp;
  63.                 for (int k = 0; temp[k] != 0; k++) {
  64.                     if (temp[k] != '^') albums[i].title[k] = temp[k];
  65.                     else albums[i].title[k] = ' ';
  66.                     last = k;
  67.                 }
  68.                 albums[i].title[last + 1] = '\0';
  69.                 fin >> temp;
  70.                 for (int k = 0; temp[k] != 0; k++) {
  71.                     if (temp[k] != '^') albums[i].artist[k] = temp[k];
  72.                     else albums[i].artist[k] = ' ';
  73.                     last = k;
  74.                 }
  75.                 albums[i].artist[last + 1] = '\0';
  76.                 fin >> albums[i].year;
  77.                 fin >> albums[i].tracks;
  78.                 fin >> albums[i].minutes;
  79.                 fin >> albums[i].singles;
  80.                 fin >> albums[i].sells;
  81.                 fin >> albums[i].mark;
  82.             }
  83.         }
  84.     }
  85.     fin.close();
  86. }
  87. int count_in_file(char file[50]) {
  88.     int count;
  89.     ifstream fin(file);
  90.     if (fin) fin >> count; else count = 0;
  91.  
  92.     return count;
  93. }
  94. //Очистить базу
  95. void clear(playlist *albums, int count, char file[50]) {
  96.     if (count > 0) {
  97.         remove_DB(file);
  98.         free(albums);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement