Initially, I had to create a program to save the data of a struct to a file. The struct is as follows:
typedef struct
{
char nome[30];
int matricula;
char conceito;
} TipoAluno;
I made the program in a way that saved the data in the file as follows:
Nome: Maria da Silva
Matricula: 2016010203
Conceito: A
Nome: Joana Oliveira
Matricula: 2015020301
Conceito: A
Nome: Joao Souza
Matricula: 2017050401
Conceito: B
Nome: Paulo Silveira
Matricula: 2015020301
Conceito: A
Nome: Hugo Fernandes
Matricula: 2014050102
Conceito: C
After this exercise, you were prompted to create another program to read only the odd records in that file. But I was in doubt. Is it possible to use the% cc% index notation to manipulate the contents of the file and write the code or is it only possible using the file information (for example, lines or characters)?
I made the following code:
int main()
{
FILE *arq;
char c;
int contaLinha = 1;
arq = fopen("Teste.txt", "r");
if(arq == NULL)
{
printf("Erro ao abrir o arquivo.");
exit(1);
}
while(!feof(arq))
{
c = fgetc(arq);
if(c == '\n')
contaLinha++;
if(contaLinha == contaLinha || contaLinha + 8)
printf("%c", c);
}
fclose(arq);
return 0;
}
I thought that way, every 8 lines, there is an odd record. However, it is necessary to create a stop condition to stop reading the even registers. How could this stop be?