'Add delimiter when writing a txt file

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


Solution 1:[1]

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);

Solution 2:[2]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MikeCAT
Solution 2 Primitive