abC primeri - datoteke/fscanf2.c

fscanf2.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// #include <memory.h>

typedef struct student {
  int SID;
  int ocena_vaj;
  int ocena_izpita;
  float povprecje;

  struct student *nasl;
} stip;

stip *zacetek = NULL;

void dodaj(int id, int ov, int oi) {
  stip *nov, *tr;
  nov = (stip *) malloc(sizeof(stip));
  nov->SID          = id;
  nov->ocena_izpita = oi;
  nov->ocena_vaj    = ov;
  nov->povprecje    = (ov + oi) /2;

  if (zacetek == NULL) {
	zacetek = nov;
    nov ->nasl = NULL;
  } else {
    if (zacetek->povprecje < nov->povprecje) {
	  nov ->nasl = zacetek;
	  zacetek = nov;
	} else {
	  tr = zacetek;
	  while ((tr->nasl != NULL) && 
            (tr->nasl->povprecje > nov->povprecje))
		tr = tr ->nasl;
	  nov -> nasl = tr ->nasl;
	  tr -> nasl = nov;
	}
  }
}

void izpisi() {
  stip *tr;
  tr = zacetek;
  while (tr != NULL) {
    printf("%d: %d, %d, %.2f\n", tr->SID, 
      tr -> ocena_vaj, tr->ocena_izpita, tr->povprecje);
	tr = tr -> nasl;
  }
}

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

  int tsid, tov, toi;
  int 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 {
      dodaj(tsid,tov,toi);
    }
  } 
  fclose(d);
  
  izpisi();
  return 0;
}

    Nazaj...