file operations -> console crashing

Hi y'all, I'm a c++ student and was practicing file operations this morning, when a problem caught my attention.
I was coding a program which should have read informations from a simple text file, and then should have printed them on screen. The informations are stored in an array of structured data, containing multiple arrays for each information. By removing lines of code to check which was causing my problem, I found out that the while loop is causing the crash. I really don't know the reason. Please help me, and thanks in advance.

THIS IS THE main.cpp FILE:
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
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
  typedef struct player
	{
		char name[20];
		char surname[20];
		char age[2];
		char score[4];
	}t_player;	
	
	int i=0;	
	
	t_player players[10];
	
	ifstream file1;
	file1.open("highscores.txt");	

	if (!file1)
	{
		cout<<endl<<"Error!";
		return -1;
	}

  	while (!file1.eof())
	{
		file1.getline(players[i].name,20);
		file1.getline(players[i].surname,20);
		file1.getline(players[i].age,2);
		file1.getline(players[i].score,4);
		i++;
	}
	
	int numero_giocatori = i;
	
	for(i=0 ; i<numero_giocatori ; i++)
	{
		cout<<endl<<"NAME: "<<players[i].name;
		cout<<endl<<"SURNAME: "<<players[i].surname;
		cout<<endl<<"AGE: "<<players[i].age;
		cout<<endl<<"-SCORE: "<<players[i].score;
		cout<<endl;
	}

	file1.close();
	return 0;
}


THIS IS THE TEXT FILE:
1
2
3
4
5
6
7
8
9
john
johnson
44
1900

Philip
Philips
25
895
Last edited on
Let's start:
1) age and score arrays are too small: you need at least 3 character array to stor 2 character c-string (do not forget about trailing zero)
2) Empty line between records are going to mess you up. Either remove it or add respective handling into your read routine.
3) !file1.eof() How many people made same mistake... In your case you should make end of file handling entirely different (and also add error proofing as a side benefit)

4) typedef struct player {/*...*/} t_player; This is C++. You can simply do struct t_player {/*...*/};
Looping in eof() is bad

change the loop to :
1
2
3
4
5
6
7
while( i < 10 && // prevent buffer overflow
    file1.getline( players[i].name, 20 ) &&
    file1.getline( players[i].surname, 20 ) &&
    file1.getline( players[i].age, N )  &&   // why are u storing age & score to a char [] ?
    file1.getline( players[i].score, N ) &&
    file1.ignore( 10, '\n' ) )   // discard the blank line (i put 10 just to make sure; you can ommit the arguments entirely)
    ++i ;

Last edited on
Slightly modified nvrmnd code (his code will lose last record if file last line is not empty):
1
2
3
4
5
6
7
8
9
10
11
12
13
{
    t_player temp;
    while( i < 10 &&
           file1.getline( temp.name, 20 ) &&
           file1.getline( temp.surname, 20 ) &&
           file1.getline( temp.age, 2 )  && 
           file1.getline( temp.score, 4 )
         ) {
        players[i] = temp;
        ++i;
        file1.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
}
Last edited on
Topic archived. No new replies allowed.