Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "file.h"
- //Create new file
- ofstream new_DB(char file[50]) {
- ofstream ofile(file);
- return ofile;
- }
- //Remove file
- void remove_DB(char file[50]) {
- ifstream fin(file);
- if (fin.is_open()) {
- fin.close();
- if (remove(file)) {
- cout << "Error removing file\n";
- return;
- }
- cout << "Успех! Файл базы успешно удален.\n";
- }
- }
- void save_DB(playlist *albums, int count, char file[50]) {
- ofstream four = new_DB(file);
- four << count << endl;
- char temp[101]; int last = 0;
- for (int i = 0; i < count; i++) {
- for (int k = 0; albums[i].title[k] != 0; k++) {
- if (albums[i].title[k] != ' ') temp[k] = albums[i].title[k];
- else temp[k] = '^';
- last = k;
- }
- temp[last + 1] = '\0';
- four << temp << endl;
- for (int k = 0; albums[i].artist[k] != 0; k++) {
- if (albums[i].artist[k] != ' ') temp[k] = albums[i].artist[k];
- else temp[k] = '^';
- last = k;
- }
- temp[last + 1] = '\0';
- four << temp << endl;
- four << albums[i].year << endl;
- four << albums[i].tracks << endl;
- four << albums[i].minutes << endl;
- four << albums[i].singles << endl;
- four << albums[i].sells << endl;
- four << albums[i].mark << endl;
- }
- four.close();
- cout << "Запись в файл прошла успешно!";
- }
- void load_DB(playlist *albums, char file[50], int count) {
- ifstream fin(file);
- if (!fin.is_open()) {
- new_DB(file);
- } else {
- fin >> count;
- char temp[101]; int last = 0;
- if (fin) {
- for (int i = 0; i < count; i++) {
- fin >> temp;
- for (int k = 0; temp[k] != 0; k++) {
- if (temp[k] != '^') albums[i].title[k] = temp[k];
- else albums[i].title[k] = ' ';
- last = k;
- }
- albums[i].title[last + 1] = '\0';
- fin >> temp;
- for (int k = 0; temp[k] != 0; k++) {
- if (temp[k] != '^') albums[i].artist[k] = temp[k];
- else albums[i].artist[k] = ' ';
- last = k;
- }
- albums[i].artist[last + 1] = '\0';
- fin >> albums[i].year;
- fin >> albums[i].tracks;
- fin >> albums[i].minutes;
- fin >> albums[i].singles;
- fin >> albums[i].sells;
- fin >> albums[i].mark;
- }
- }
- }
- fin.close();
- }
- int count_in_file(char file[50]) {
- int count;
- ifstream fin(file);
- if (fin) fin >> count; else count = 0;
- return count;
- }
- //Очистить базу
- void clear(playlist *albums, int count, char file[50]) {
- if (count > 0) {
- remove_DB(file);
- free(albums);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement