0
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <string>
#include <iostream>
#include <conio.h>
#include <clocale>
#include <stdlib.h>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <fstream>

using namespace std;
struct SongStruct {

    char authorSongs[60];
    char nameSong[60];
    char yearCreat[100];
    char LyricsOfSong[5000];

};

void Menu() {
    cout << "\t\t\t\tLyrics catalogue" << endl;
    cout << "\t\t\t\t1-Add text of songs" << endl;
    //1 - Текст может быть введен с клавиатуры
    //2 - Загружен из файла
    cout << "\t\t\t\t2-Delete text of song." << endl;
    //1 - Удаление песни
}
    

SongStruct* AddStruct(SongStruct* Obj, const int SongAmount)
{


    if (SongAmount == 0)
    {
        Obj = new SongStruct[SongAmount+1];
    }
    else
    {
        SongStruct* tempObj = new SongStruct[SongAmount + 1];

        for (int i = 0; i < SongAmount; i++)
        {
            tempObj[i] = Obj[i];
        }
        delete[] Obj;

        Obj = tempObj;
    }
    return Obj;

}
SongStruct* AddToStruct(SongStruct* OldObj, SongStruct* Obj, const int SongAmount)
{
    SongStruct* tempObj = new SongStruct[SongAmount + 1];

    for (int i = 0; i < SongAmount; i++) {
        strcpy_s(tempObj[i].authorSongs, 60, OldObj[i].authorSongs);
        strcpy_s(tempObj[i].nameSong, 60, OldObj[i].nameSong);
        strcpy_s(tempObj[i].yearCreat, 100, OldObj[i].yearCreat);
        strcpy_s(tempObj[i].LyricsOfSong, 5000, OldObj[i].LyricsOfSong);
    }
    strcpy_s(tempObj[SongAmount + 1].authorSongs, 60, Obj->authorSongs);
    strcpy_s(tempObj[SongAmount + 1].nameSong, 60, Obj->nameSong);
    strcpy_s(tempObj[SongAmount + 1].yearCreat, 100, Obj->yearCreat);
    strcpy_s(tempObj[SongAmount + 1].LyricsOfSong, 5000, Obj->LyricsOfSong);
    delete[] OldObj;

    return tempObj;

}

void GetData(SongStruct* Obj, const int SongAmount) {
    string path = "myCatalogSong.txt";
    ofstream record;
    record.open(path, ios_base::app);

    if (!record.is_open())
    {
        cout << "Error open file!" << endl;
    }
    else {
        
        cout << "Author:";
        cin.getline(Obj[SongAmount].authorSongs, 60);       
        cout << "Song name:";
        cin.getline(Obj[SongAmount].nameSong, 60);      
        cout << "Year:";
        cin.getline(Obj[SongAmount].yearCreat, 100);
        cout << "Lyrics of song:";
        cin.getline(Obj[SongAmount].LyricsOfSong, 5000);
        
        record <<"Author:" << Obj[SongAmount].authorSongs  <<
            "Song name: " << Obj[SongAmount].nameSong  <<
            "Year: " << Obj[SongAmount].yearCreat <<
            "Lyrics of song: " << Obj[SongAmount].LyricsOfSong;
    }
    record.close();

}
void GetData2(SongStruct* Obj, const int SongAmount) {
    string path = "myCatalogSong.txt";
    ofstream record;
    record.open(path, ios_base::app);
    if (!record.is_open())
    {
        cout << "Error open file!" << endl;
    }
    else {
        cout << "\n";
        cout << "Author: ";
        cin.getline(Obj[SongAmount].authorSongs, 60);
        cout << endl;
        cout << "Song name: ";
        cin.getline(Obj[SongAmount].nameSong, 60);
        cout << endl;
        cout << "Year: ";
        cin.getline(Obj[SongAmount].yearCreat, 100);
        cout << "\nLyrics of song: ";
        cout << "\nEnter name File load lyrics" << endl;
        string path;
        cin >> path;
        ifstream fin;
        string str;
        fin.open(path);
        if (!fin.is_open())
        {
            cout << "Error open file!" << endl;
        }
        else
        {
            cout << "File is open" << endl;
            while (!fin.eof())
            {
                str = "";
                getline(fin, str);
            }
        }

        cout << str;
        cout << endl;
        record << "\n" << "Author: " << Obj[SongAmount].authorSongs << "\n" <<
            "Song name: " << Obj[SongAmount].nameSong << "\n" <<
            "Year: " << Obj[SongAmount].yearCreat << "\n" <<
            "Lyrics of song: " << str << "\n";
    }
    record.close();
}

void ShowData(const SongStruct* Obj, const int SongAmount) {
    system("cls");
    for (int i = 0; i < SongAmount; i++) {

        cout << "Author: " << Obj[i].authorSongs << endl;
        cout << "Song name: " << Obj[i].nameSong << endl;
        cout << "Year: " << Obj[i].yearCreat << endl;
        cout << "Lyrics of song: " << Obj[i].LyricsOfSong << endl;

    }
}

void Vvod(SongStruct* arr) {
    int SongAmount = 0;
    int YesOrNot = 0;
    do
    {
        arr = AddStruct(arr, SongAmount);
        GetData(arr, SongAmount);

        SongAmount++;
        cout << endl;
        cout << "If you want to continue: 1-yes, 0-no: ";

        cin >> YesOrNot;
        cin.get();

    } while (YesOrNot != 0);
    ShowData(arr, SongAmount);
}

void Vvod2(SongStruct* arr) {
    int SongAmount = 0;
    int YesOrNot = 0;
    do
    {
        arr = AddStruct(arr, SongAmount);
        GetData2(arr, SongAmount);

        SongAmount++;
        cout << endl;
        cout << "If you want to continue: 1-yes, 0-no: ";

        cin >> YesOrNot;
        cin.get();

    } while (YesOrNot != 0);
    ShowData(arr, SongAmount);
}


void GetData3(SongStruct* arr) {
    int SongAmount = 0;
    int YesOrNot = 0;
    while (true)
    {
        arr = AddStruct(arr, SongAmount);
        GetData2(arr, SongAmount);

        SongAmount++;
        cout << endl;
        cout << "If you want to continue: 1-yes, 0-no: ";

        cin >> YesOrNot;
        cin.get();

    } while (YesOrNot != 0);
    ShowData(arr, SongAmount);
}

int strIngVchar(char*  arr,  string str) {
    for (int i = 0; i < str.length()+1; i++) {      
            arr[i]= str[i];     
    }
    return *arr;
}

int LoadFile(SongStruct* arr, int size) {
    string path = "myCatalogSong.txt";
    ifstream fin;
    fin.open(path, ifstream::out | ifstream::in );
    int i = 0;
    if (!fin.is_open())
    {
        cout << "Error open file!" << endl;
    }
    else {
        
        cout << " File is open" << endl;
        char str;
        //char str[5500];
        SongStruct* arr2 = new SongStruct;

        while(fin.get(str))
        {           
              i++;
            arr = AddStruct(arr, i);
              cout << str;          
            
        }
    }
    fin.close();    
    return i;
}




void OutPutArray(SongStruct* arr, int size) {

    
    
    for (int i = 0; i < size; i++) {

        cout << "Author: " << arr[i].authorSongs << endl;
        cout << "Song name: " << arr[i].nameSong << endl;
        cout << "Year: " << arr[i].yearCreat << endl;
        cout << "Lyrics of song: " << arr[i].LyricsOfSong << endl;

    }
        
        
        

    
    
    
    
}


int main() {

    int size = 1;
    SongStruct* arr = new SongStruct[size];
      size=LoadFile(arr,size);    
     OutPutArray(arr,size);
    
    Menu();

    int z = 0;
    while (true) {
        cin >> z;
        switch (z)
        case 1: {
            cin.ignore();
            Vvod(arr);
            break;
        case 2:
            cin.ignore();
            Vvod2(arr);
            break;
        }

    }

    delete[] arr;
    /*
    string path= "CatalogOfSongs.txt";
    //cin >> path;   самому ввести путь к файлу
    ofstream songs;
    songs.open(path);

    if (!songs.is_open())
    {
        cout << "Error opens file!" << endl;
    }
    else
    {
        cout << "Enter what you ask" << endl;
        int a;
        cin >> a;
        songs << a;
        songs << "\nThis is new song!";
    }

    songs.close();
    */

    return 0;
}

вот так выглядит структура записанная каждая строчка это массив char 

Author: Britney
Song name: Oops!
Year: 1998
Lyrics of song: TextSong

11
  • файл что содержит? 24 дек 2020 в 13:31
  • текстовый файл содержит вот такие записи. Author: Britney Song name: Oops Year: 1995 Lyrics of song: Text Song 24 дек 2020 в 13:38
  • дополните куском содержимого файла вопрос 24 дек 2020 в 13:39
  • Он постоянно выводит мусор (толи я с индексами неправильно работаю толи вообще всё неправильно сделал) 24 дек 2020 в 13:40
  • Куском содержимого куда дополнить, в код написать ? 24 дек 2020 в 13:42

1 ответ 1

0
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;
using std::cout;
using std::ifstream;

struct SongStruct
{
    char authorSongs[25];
    char nameSong[25];
    char yearCreat[25];
    char LyricsOfSong[25]; 
};

int main()
{

  SongStruct ss;
  string path = "myCatalogSong.txt";

  ifstream fin;
  fin.open(path, ifstream::out | ifstream::in | ifstream::binary);

  if (!fin.is_open()) {
    cout << "Error open file!" << endl;
  }

  string str;
  
  getline(fin, str);
  strcpy(ss.authorSongs, str.c_str());
  getline(fin, str);
  strcpy(ss.nameSong, str.c_str());
  getline(fin, str);
  strcpy(ss.yearCreat, str.c_str());
  getline(fin, str);
  strcpy(ss.LyricsOfSong, str.c_str());

  fin.close();

    cout << ss.authorSongs << endl;
    cout << ss.nameSong << endl;
    cout << ss.yearCreat << endl;
    cout << ss.LyricsOfSong << endl;

   return 0;
}

введите сюда описание изображения

введите сюда описание изображения

4
  • Круто!!! Спасибо за помощь!!! Попытаюсь ещё всё динамически сделать ) 24 дек 2020 в 16:18
  • Динамически все равно не хочет. Но статически работает. То есть здесь получается я одну запись могу вывести. а у меня там массив структур. 24 дек 2020 в 16:49
  • нужен короткий пример кода с указанной проблемой без лишнего меню, может тогда кто-то ответит 24 дек 2020 в 17:39
  • у меня меню 4 не сработало 24 дек 2020 в 17:40

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.