File Function in C ++

0

Hello, I have a problem using the file function, I need to make a program that registers the place and the name of the person on a bus, but the problem is that every time I close the program and open the name and place again the person chose is reset, I would like to know how do I get it saved in txt without resetting again. PS: I'm new to programming.

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int op=-1;

struct tabela{
    int flag=0,lugar=0;
    char nome[20];
};
struct tabela A[20],B[20];
void verificarlugarA();
void verificarlugarB();
void preencherlugarA();
void preencherlugarB();
void readbusao();
void writebusao();
int main(){

    readbusao();
    cout<<"Para escolher a fileira A digite 1 \nPara escolher a fileira B digite 2\npara sair digite 0"<<endl;
    cin>>op;
    if(op==0){
        writebusao();
        exit(0);
    }
    switch (op){
    case 1:
        verificarlugarA();
        preencherlugarA();
        writebusao();
        main();
    case 2:
        verificarlugarB();
        preencherlugarB();
        writebusao();
        main();
    }
}
void verificarlugarA(){
    for(int k=0;k<20;k++){
        if (A[k].flag==0){
            cout<<"fileira A na posicao "<<k<<" esta vazio"<<endl;
        }
    }
}
void verificarlugarB(){
    for(int k=0;k<20;k++){
        if (B[k].flag==0){
            cout<<"fileira B na posicao "<<k<<" esta vazio"<<endl;
        }else{
        }
    }
}
void preencherlugarA(){
    int lg=0,ct=0;
    cout<<"escolha um lugar"<<endl;
    while(1){
        if(ct>0){
            cout<<"escolha outro lugar"<<endl;
        }
        cin>>lg;
        if(A[lg].flag==0){
            A[lg].flag=1;
            cout<<"coloque o seu nome"<<endl;
            cin>>A[lg].nome;
            break;
        }else{
            ct++;
            }
        }
    }

void preencherlugarB(){
    int lg=0,ct=0;
    cout<<"escolha o seu lugar"<<endl;
    while(1){
        if(ct>0){
            cout<<"escolha outro lugar"<<endl;
        }
        cin>>lg;
        if(B[lg].flag==0){
            B[lg].flag=1;
            cout<<"coloque o seu nome"<<endl;
            cin>>B[lg].nome;
            break;
        }else{
            ct++;
            }
        }
    }

void readbusao(){
FILE*busao;
        if((busao=fopen("busao.txt","r"))==NULL){
            cout<<"Erro ao abrir o arquivo"<<endl;
        }
        for(int k=0;k<20;k++){
            fscanf(busao,"Fileira A %d %d %s",&A[k].flag,&A[k].lugar,&A[k].nome);
        }
        for(int k=0;k<20;k++){
            fscanf(busao,"Fileira B %d %d %s",&B[k].flag,&B[k].lugar,&B[k].nome);
        }
        fclose(busao);
}
void writebusao(){
FILE*busao;
        if ((busao = fopen("busao.txt","w"))==NULL){
            cout<<"Problema para abrir"<<endl;
        }

        for(int k=0;k<20;k++){
            fprintf(busao,"Fileira A %d %d %s\n",A[k].flag,k, A[k].nome);
        }
        cout<<endl;
        for(int k=0;k<20;k++){
            fprintf(busao,"Fileira B %d %d %s\n",B[k].flag,k, B[k].nome);
        }
        fclose(busao);
}
    
asked by anonymous 17.08.2018 / 22:48

1 answer

0

Matheus, I tested your code and it worked. Except for this stretch.

int flag=0,lugar=0;

I changed to:

int flag,lugar;

You may not be able to read or write to the root directory

    
17.08.2018 / 23:40