abC primeri - datoteke/fscanf1.c

fscanf1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *args[]) {
  FILE *d;
  if ((d = fopen("ocene.txt", "r")) == NULL)
	exit(2);

  // prebrane vrednosti trenutnega studenta
  int tov, toi, tsid;
  
  // vsota vseh ocen in stevilo studentov
  int ov = 0, oi = 0, n = 0;
  
  int stp; // koliko podatkov je v trenutni vrstici

  while(!feof(d)) {
	stp = fscanf(d, "%d %d %d\n", &tsid, &tov, &toi);
    if (stp !=3) {
      printf("Napacen format podatkov v vrstici %d", n+1);
      exit(1);
    } else {
      n++; ov += tov;  oi += toi; 
    }
  } 
  fclose(d);

  if (n==0) 
    printf("Datoteka %s je prazna", args[1]);
  else
    printf("Vaje: %.2f, Izpit: %.2f\n", 
      1.0*ov/n, 1.0*oi/n);

  return 0;
}

    Nazaj...