I need help to add a "|"
delimiter when I write a .txt
file in my C program.
typedef struct aluno {
char nome[100];
int idade;
int nota;
}tAluno;
void escreve (char *nomeArquivo) {
tAluno aluno;
FILE *arq = fopen(nomeArquivo, "w");
for (int i = 0; i < 2; i ) {
printf("Nome: ");
scanf("%s", aluno.nome);
printf("Idade: ");
scanf("%d,", &aluno.idade);
printf("Nota: ");
scanf("%d,", &aluno.nota);
fprintf(arq, "%s", aluno.nome);
fprintf(arq, "%d", aluno.idade);
fprintf(arq, "%d", aluno.nota);
}
}
Without the demiliter my file would look somthing like this:
claudio2510natalia2518
But I want it to look like this:
claudio|25|10|natalia|10|18
CodePudding user response:
You should simply add some code to output delimiters. Be careful not to add delimiters before the first element nor after the last element.
if (i > 0) fprintf(arq, "|");
fprintf(arq, "%s|", aluno.nome);
fprintf(arq, "%d|", aluno.idade);
fprintf(arq, "%d", aluno.nota);
CodePudding user response:
Just add a |
to the printf and fprintf functions
The %s is only used by printf to determine where to put the first string argument.
fprintf(arq, "%s | ", var);
printf("%s |", var);