Remove data from a C++ file

Asked

Viewed 1,456 times

0

I’m making a database to store names of rock bands. But I’m not able to implement band 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 is just not removing the data from the file. In case the registered bands

  • 2

    What problem does the code present ? Gives error ? If yes which ?

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

  • I haven’t tested your code yet I have at least two things to say. You’re always fiddling like everything else right in the archive, and that’s usually not a good idea for all sorts of reasons. It is better to read everything for memory, do all the manipulations you want in memory and when it is necessary to write to archive. This greatly simplifies things like changing 1 record only. Another point you got wrong in your code is that in function remover you open the same file for reading and writing simultaneously. This is wrong and will not work properly.

1 answer

1

Hello Rafael is welcome.

A solution to your problem would be for you to copy everything into a new file. txt minus the line/band you want to take, then delete the . txt old and rename the new . txt to the old name.

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

Another solution would be to place the data in an SQL database, where each record can be updated/removed independently.

Here is an example of a code to delete something from a txt:

Delete line from a C file

Deleting specific line from a text file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.