Remove data from a C ++ file

0

I'm making a database for storing rock band names. But I'm not able to implement strip removal separately. Can anyone help? Follow the code.

#include <iostream>
#include <fstream>

using namespace std;

void banda(){
    ifstream arquivo("layout.txt");
    if(arquivo){
        string bandas;
    while(getline(arquivo,bandas)){
        cout<<bandas<<endl;
    }
    arquivo.close();
    }
    else{
        cout<<"erro";
    }
}

void inserir(){
    ofstream arquivo("bandass.txt", ios::app);
    if(arquivo){
        int a;
        string g;
        int b;
        string c;
        string nome;
        cout<<"nome da banda"<<endl;
        cin>>nome;
        arquivo<<nome<<endl;

        cout<<"genero"<<endl;
        cin>>g;
        arquivo<<"Genero: "<<g<<endl;

        cout<<"ano de criacao"<<endl;
        cin>>a;
        arquivo<<"Ano de criacao: "<<a<<endl;

        cout<<"ano de encerramento"<<endl;
        cin>>b;
        arquivo<<"Ano de enceramento: "<<b<<endl;

        cout<<"descrição"<<endl;
        cin>>c;
        arquivo<<"Descricao: "<<c<<endl;

    arquivo.close();
    }
    else{
        cout<<"erro";
    }
}

void remover(){
    ifstream arquivos("bandas.txt");
    ofstream arquivon("bandas.txt");
    string c;
    cout<<"Digite o nome da banda que deseja remover"<<endl;
    cin>>c;
    string aux;
    if(arquivos and arquivon){
        while (arquivos>>aux){
            if ( aux!= c ){
                arquivon<<aux;
                }
        }
    }
}
void buscar(){

    string a;
    cout<<"Digite o nome da banda desejada"<<endl;
    cin>>a;
    ifstream arquivo("bandass.txt");
    string aux;
    bool x=false;
    if(arquivo){
        while( arquivo>>aux){
            if( aux==a){
                cout<<"Essa banda encontra-se no arquivo"<<endl;
                x=true;
                }
            }
    arquivo.close();
    }
    if( x==false){
        cout<<"Banda nao encontrada no Arquivo."<<endl;
        cout<<"Tenha certeza que escreveu o nome corretamente"<<endl;
        }
}

void exibir(){
    ifstream arquivo("bandass.txt");
    if(arquivo){
        string bandas;
    while(getline(arquivo,bandas)){
        cout<<bandas<<endl;
    }
    arquivo.close();
    }
    else{
        cout<<"erro";
    }
}
int main(int argc, char **argv)
{
    bool n=false;
    char x;

    while( n==false){

        banda(); //cabeçario 

        cout<<"Inserir uma letra"<<endl;

        cin>>x; //opção escolhida

        if( x=='i' or x=='I'){
            inserir();

            char u;
            cout<<"Deseja retornar ao menu? Se nao, pressione a letra n, caso contrario letra s"<<endl;
            cin>>u;
            if( u=='n' or u=='N' ){
                n=true;
            }
        }
        else if( x=='r' or x=='R'){
            remover();
        }
        else if( x=='b' or x=='B'){
            buscar();
        }
        else if( x=='e' or x=='E'){
            exibir();
        }
        else if( x=='s' or x=='S'){
            n=true;
        }
        else{
            cout<<"Foi inserido uma letra nao existente no menu. Deseja continuar? Se nao, pressione a letra n, caso contrario aperte qualquer letra"<<endl;
            char y;
            cin>>y;
            if( y=='n' or y=='N'){
                n=true;
            }
        }
    }


    return 0;
}

No error, it just is not removing the data from the file. In the case of the registered bands

    
asked by anonymous 18.10.2018 / 23:47

1 answer

0

Hello Rafael, welcome.

One solution to your problem would be to copy everything into a new .txt file minus the line / band you want to remove, then delete the old .txt and rename the new .txt to the old one.

Let's say in passing that this solution does not apply only in C / C ++ / C # - when dealing with text files / binders in .txt, in any language, the only way to create changes is to write a new file.

Another solution would be to put the data on a SQL database, where each record can be updated / removed independently.

Here is an example code for deleting something from a txt:

Delete line from a file in C

Delete specific line from a text file

    
19.10.2018 / 02:16