0
I am starting in C and I have the following problem. I need to save strings in a txt file sorted in stack, so far so good, but I need that when I open the program again, it keeps stacking always at the top, however it is overwriting what was already stored. I made the code as simple as possible.
Here I store the string at the 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 store in the file . txt 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();
}
The
"%[...]"
ofscanf()
does not lead"s"
(the[
by itself already indicates that you want to read a string).– pmg
What is the meaning of this Rewind inside the loop?
– anonimo