Saving txt files with c++

Asked

Viewed 451 times

0

I am trying to create a program that saves data stored with hash in 4 different txt files, depending on the index of the hash it chooses the file to save.

example:

hash índice 0 = txt_0.txt
hash índice 1 = txt_1.txt
hash índice 2 = txt_2.txt
hash índice 3 = txt_3.txt

So I created a for that goes through all my list, and created a Literator to be able to rescue the data from the list and save in txt, the problem and that it only saves if I register a later saved, if register 2 it only saves the last registered.

void func_arquivo::cria_arquivo(int código, int idade, string nome, int     indice){
ofstream arquivo;
for(int i=0;i<TAM;i++){
    char nome_arquivo[FILENAME_MAX];//usado para que a função consiga escrever todos os nomes dentro do char se nao ele nao comporta
    sprintf(nome_arquivo, "Data-%d.txt",i);//função que realiza conversao de inteiros para char
    arquivo.open(nome_arquivo, ios::app);

    if(indice == i){
        arquivo << codigo << "\t" << idade << "\t" <<  nome << "\n";
    }
    arquivo.close();
 }
}

This is the code you write in txt.

I wonder what happens?

I’m kind of lost in it.

  • 1

    If you will create a file according to the indice, the for maybe it’s unnecessary, I tested your code here and it seems to work...

  • Hello Originated for trying to help but actually was missing a for so just save the last element inserted it did not read the entire list only the last value.

  • But what list is this? Something else, what are you calling cria_arquivo?

No answers

Browser other questions tagged

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