I am learnin how to program in C, and I've been having a hard time with using files.
How can I do for example, there are two files. On it There are names and grades, from 1 to 10 of students. Like:
John 10 John 5
Alex 6 Alex 9
Mary 8 Mary 6
How do I get to acess specific numbers, from a given student and add the numbers for example, do I have to use fseek , SEEK_END, or should i use ftell? And take the median of all?
How should the code be?
edit1: What I tried(sorry about the variables in portuguese, I added some comments):
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char nome[30];
float media;
char situacao;
}informa;
// function to create files \/
int cria_arquivo(){
informa A;
char nome[30];
float n1, n2;
`FILE *arq1, *arq2, *arq3, *arq4; //pointers to archives students, grades,` and anothe file to print on
arq1 = fopen("alunos.txt","r"); // students and grades
arq2 = fopen("notas1.txt","r");
arq3 = fopen("notas2.txt","r");
arq4 = fopen("turma.dat", "wb");
if((arq1 == NULL) || (arq2 == NULL) || (arq3 == NULL) || (arq4 == NULL)){
printf("FATAL ERROR - NAO E POSSIVEL ABRIR ARQUIVOS"); //cant open the files
exit(1);
}
while(1){
//printf("ola");
fscanf(arq1, "%s", nome);
printf("%s\n", nome);
if (feof(arq1)) {
break;
}
//printf("Funciona");
fscanf(arq2, "%f", &n1);
fscanf(arq3, "%f", &n2);
strcpy(A.nome, nome);
A.media = (n1+n2)/2;
printf("%f\n", A.media);
if(A.media < 5){
A.situacao = 'F';
}
else{
A.situacao = 'A';
}
fwrite(&A, sizeof(informa), 1, arq4);
}
fclose(arq1); fclose(arq2); fclose(arq3); fclose(arq4);
return 0;
}
int main(){
cria_arquivo();
return 0;
}