While loop

Hello, I have a problem with the while loop.

1
2
3
    while(!feof(f)) {
    ucitajStudente(f, &(studenti[brStudenta]));
    }


Instead of running the function until it is the end of the file, the loop is running infinity. Why?

Thanks,
Igor
What does ucitajStudente do ? Do you actually read from the file ?
Yes, it's reading data from the .txt file

Here is the full code:

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
#include <stdio.h>
#define MAXVREDNOST 250

typedef struct prijemni{
	int brLista;
	int brPrijave;
	char ime[32];
	char prezime[32];
	double bodoviSrednja;
	int bodoviPrijemni;
	double bodoviUkupan;
} student;

//promenljive
student studenti[MAXVREDNOST];
int brStudenta = 0;

double ucitajStudente(FILE *f, student *s){
	fscanf(f, "%d %d %s %s %fl %d %fl", &(s->brLista), &(s->brPrijave), &(s->ime), &(s->prezime), &(s->bodoviSrednja), &(s->bodoviPrijemni), &(s->bodoviUkupan));
}

void stampajStudente(){
	int i;
	for(i=0; i<brStudenta;i++){
	printf(" %d %d %s %s %f %d %f", studenti[i].brLista, studenti[i].brPrijave, studenti[i].ime, studenti[i].prezime, studenti[i].bodoviSrednja, studenti[i].bodoviPrijemni, studenti[i].bodoviUkupan);
}
}

main(){
	
	FILE *f;
	f = fopen("prijemni.txt", "r");
	if (f==NULL){
		printf("Greska!");
		return -1;
	}
	
	while(feof(f)) {
		ucitajStudente(f, &(studenti[brStudenta]));
		brStudenta++;
	}
	
	
	stampajStudente();
	
	fclose(f);
	

}
Hi,
1
2
3
4
while(feof(f)) {
		ucitajStudente(f, &(studenti[brStudenta]));
		brStudenta++;
	}

I am not very sure why this loops forever in the first place. Maybe your input file is empty (or your program doesn't have permission to access the file).

However, you may try this :
1
2
3
4
5
6
7
std::cout << ((!feof(f)) ? "Start reading..." : "Error : File is empty (EOF)") << "\n\n";

while(!feof(f)) {
		ucitajStudente(f, &(studenti[brStudenta]));
		brStudenta++;
	std::cout << "#" << brStudenta << " student(s) read" << std::endl; 
}

Last edited on
Can you let us see the content of the file prijemni.txt?

Edit : Let us know your program output if there is one.
Last edited on
Yes, here is the prijemni.txt:

1 103 John Smith 39.14 60 99.14
2 183 John Doe 38.58 60 98.58
3 41 Bernard Ames 38.16 60 98.16
4 88 Timothy Morris 38.08 60 98.08
5 124 Christopher Moss 37.44 60 97.44
6 347 Tim Bitner 37.24 60 97.24

There is not program output, just a blank CMD window since while is running infinity.
Thanks
Ok, did you really ran my solution?

Anyway, I have updated it. Let me know your output outcome.
fscanf(f, "%d %d %s %s %fl %d %fl", &(s->brLista), &(s->brPrijave), &(s->ime), &(s->prezime), &(s->bodoviSrednja), &(s->bodoviPrijemni), &(s->bodoviUkupan));
It is not %fl. It is %lf. Also put a space character at the end of the format string, which tells fscanf() to eat any whitespace characters, including new-lines ('\n')

So :
fscanf(f, "%d %d %s %s %lf %d %lf ", &(s->brLista), &(s->brPrijave), &(s->ime), &(s->prezime), &(s->bodoviSrednja), &(s->bodoviPrijemni), &(s->bodoviUkupan));
Thank you so much, the fscanf part was the only problem.
It works now and I get my results. Thanks again!

Have a great day!
Oh, glad it helped :)
Topic archived. No new replies allowed.