I'm starting in C and I have the following problem. I need to save strings to a txt file sorted in stack, so far so good, however I need it when I open the program again, it keeps stacking always at the top, but it is overwriting what was already stored. I made the code as simple as possible.
Here I store the string in top position.
void getMensagem(void){
if (topo == maxL){
printf("Pilha cheia.\n");
}
else {
printf("Digite sua mensagem:\n");
scanf("%[^\n]s",mensagem[topo]); // Lê String até encontrar o ENTER.
setbuf(stdin, NULL);
}
empilhar();
}
And then I store it in the .txt file in order:
char empilhar(){
FILE *arquivo;
arquivo = fopen(LOG, "r+");
int aux;
for(aux = topo - 1; aux != -1; aux--) {
printf("Na posicao %d temos %s\n", aux, mensagem[aux]); /// visualizar pilha
fprintf(arquivo,"%s\n",mensagem[aux]);
rewind(arquivo);
}
fclose(arquivo);
getchar();
}