-3

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;

}
  • The file you work with is a nicely structured text file. Read about `fscanf` and use it. – DYZ Feb 11 '17 at 19:33
  • It is unclear what you are asking. Further, it is unclear what the data looks like. This looks like homework, which is fine, but you'll need to be more clear about the problem, and what you have already done to solve it. – aghast Feb 11 '17 at 19:34
  • With text files it's often impossible to seek to specific positions for data, since elements in text files are generally *variable width*. For text files, one usually have to read the whole file, parsing each record until the wanted is found. – Some programmer dude Feb 11 '17 at 19:34
  • It's not homework, I am trying to learn the stuff, and trying to start with basics, to have more strong knowledge, trying to put the code here. – Fernando Gardeazabal Feb 11 '17 at 19:41

1 Answers1

0

I'm kinda confused about your question but assuming there is only one file (arq, aluno.txt) and in every line there is one name and one grade, like for example:

   Lukas 10
   Matthias 8
   Sven 5
   Fernando 10

And you wanna know just Fernando grade you can do something like this:

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>

   int main () {
      FILE *arq;
      arq = fopen ("aluno.txt", "rt");
      if (arq == NULL) {
         printf ("ERRO 404\n");
      }
      char name[50];
      int grade;
      while (fscanf (arq, "%s %d\n", &name, &grade) != EOF) {
          if(name = "Fernando"){
             //do what you want
             if(grade < 6){
                printf("Not good Fernando");
             }
          }
      }
   }

The good stuff about this method is that it will run line by line and get the names and grades of each line, until it gets to the end. Because what the fscanf() function does is gets the information of every line and then moves to the next line and return EOF when is the end of the file.

Lukas Belck
  • 30
  • 2
  • 8