I'm working on a soccer management program
I had some other members of my group to compile data on 400 real life players into a text file.
I planned on reading the text file using a program ,converting individual players into objects and then writing them to another file (in binary mode) to be used by my program..
The reading and writing seems to have gone well, but when I try to open the new file containing the player objects, the program seems to be able to read only 20 players for some reason..

#include <fstream>

#include <iostream>

using namespace std;


int main()

{
  class plr
    {
    public:
      int id;
      int shirt_no;
      int age;
      int goals;
      int pos;//0-GK,1-DEF,2-MID,3-ATT
      char LName[51];
      char FName[51];
      int team;
      int skill;
      int goaltime;
      int cards;
      void transfer(int t_id)
      {
        team = t_id;
      }
    }plrs[401],plrs2[401];



  fstream reader("players.txt", ios::in);
  fstream binary("players2.dat", ios::out);

  //READ FROM TEXT FILE into objects  :
int j;
  for (int i=1;i<=401;i++)
    {
      reader>>j;
      plrs[j].id = j;
      reader>>plrs[j].LName>>plrs[j].FName>>plrs[j].team>>plrs[j].shirt_no>>plrs[j].age>>plrs[j].pos>>plrs[j].skill>>plrs[j].goaltime>>plrs[j].cards;


      cout << "\n\n";
    }
  reader.close();

//Display all players
for(int j=1;j<=400;j++)
{cout<<"\n\n"<<plrs[j].LName<<"\n"<<plrs[j].FName<<"\n"<<plrs[j].team<<"\n"<<plrs[j].shirt_no<<"\n"<<plrs[j].age<<"\n"<<plrs[j].pos<<"\n"<<plrs[j].skill<<"\n"<<plrs[j].goaltime<<"\n"<<plrs[j].cards;
}

//Write objects to file
for (int j=1;j<=400;j++){
binary.write((char*)&plrs[j], sizeof(plrs[j]));
}
  binary.close();


//Read objects from that file through another filestream into a different array of objects
  fstream plrreader("players2.dat", ios::in);
  for (int j=1;j<=400;j++)
    {
      if (plrreader.read((char*)&plrs2[j], sizeof(plrs2[j])))
        {
          cout<<j<<" succesful\n";
        }


    }

  cout<<"Read successful";


//Display objects -- some error here, only 20 players seem to be readable :
  for (int j=1; j<400; j++)
    {
      cout<<"\n"<<plrs2[j].id<<"\nName :"<<plrs2[j].FName<<" "<<plrs2[j].LName<<"\nTeam :"<<plrs2[j].team<<"\n No.:"<<plrs2[j].shirt_no<<"\n Age:"<<plrs2[j].age<<"\n Pos :"<<plrs2[j].pos<<"\n Skill:"<<plrs2[j].skill<<"\n Max goals scored in"<<plrs2[j].goaltime<<"\n Cards"<<plrs2[j].cards;

    }
  plrreader.close();

  return 0;


}

I'm reading from players.txt, storing players in an array of 'plr' objects (plrs).
This seems to be working as expected (all 400 players are displayed correctly)

Then I'm writing these objects using write() to players2.dat. This too, seems to be working (I opened the dat file in notepad, and it has all 400 players)

But when I open the newly created dat file and read the objects into another array of player objects (plrs2), only the first 20 players are read correctly.. The rest are displayed as zeroes..

I've also tried putting the part which reads the objects (line 65 onwards) into another .cpp file, but I get the same result.

I've attached the players.txt file, if anyone needs to run the code..

Please help..

Attachments
1
Ferdinand
Rio
11
5
32
1
99
0
30
2
Nani
Luis
11
17
34
2
99
6
6
3
Hargreaves
Owen
11
4
29
2
99
5
2
4
Van_der_Sar
Edwin
11
1
40
0
99
0
10
5
Rooney
Wayne
11
10
25
3
99
6
62
6
Giggs	
Ryan
11
11
37
2
99
6
27
7
Vidic
Nemanja
11
15
29
1
99
0
30
8
Scholes
Paul
11
18
36
2
99
5
83
9
Brown
Wes
11
6
31
1
99
0
31
10
Da_Silva
Fabio
11
20
20
1
99
0
0
11
Evra
Patrice
11
3
30
1
99
0
20
12
Anderson
Oliveira
11
8
22
2
99
3
3
13
Berbatov	
Dimitar
11
9
29
3
99
5
7
14
Park
Ji-sung
11
13
29
2
99
4
4
15
Tosic
Zoran
11
14
23
2
99
0
0
16
O'Shea
John
11
22
29
1
99
0
12
17
Hernandez
Javier
11
14
22
3
99
0
0
18
Owen
Michael
11
7
31
3
99
5
9
19
Valencia
Antonio
11
25
25
2
99
5
21
20
Carrick
Michael
11
16
29
2
99
1
21
21
Ivanovic
Branislav
6
2
26
1
99
0
8
22
Ballack
Michael
6
13
34
2
99
4
23
23
-
Deco
6
20
33
2
99
5
7
24
Essien
Michael
6
5
28
2
99
2
18
25
Lampard
Frank
6
8
32
2
99
6
48
26
Drogba
Didier
6
11
32
3
99
6
32
27
Terry
John
6
26
30
1
99
0
51
28
Ferreira
Paulo
6
19
31
1
99
0
11
29
Borini
Fabio
6
45
19
3
99
0
0
30
Carvalho
Ricardo
6
6
32
1
99
0
28
31
Cole
Ashley
6
3
30
1
99
0
48
32
Cole
Joe
6
10
29
2
99
5
48
33
Malouda
Florent
6
15
30
2
99
6
10
34
Zhirkov
Yuri
6
18
27
2
99
0
4
35
Cech
Petr
6
1
28
0
99
0
2
36
Anelka
Nicolas
6
39
31
3
99
4
5
37
Kalou
Salomon
6
21
25
3
99
6
3
38
-
Mikel
6
12
23
2
99
0
21
39
Benayoun
Yossi
6
10
30
2
99
6
8
40
-
Ramires
6
7
27
2
99
0
0
41
Benayoun
Yossi
9
15
30
2
99
6
8
42
Gerrard
Steven
9
8
30
2
99
5
48
43
Torres
Fernando
9
9
26
3
99
6
11
44
Kuyt
Dirk
9
18
30
3
99
6
5
45
Reina
Jose
9
25
28
0
99
0
5
46
Lucas
Leiva
9
21
23
2
99
6
11
47
Insua
Emiliano
9
22
21
1
99
0
2
48
Johnson
Glen
9
2
26
1
99
0
28
49
Ngog
David
9
24
21
3
99
6
2
50
Babel
Ryan
9
19
24
3
99
6
4
51
Carragher
Jamie
9
23
32
1
99
0
61
52
Maxi
Rodriguez
9
17
29
2
99
5
1
53
Aquilani
Alberto
9
4
26
2
99
3
0
54
Kyrgiakos
Sotirios
9
16
31
1
99
0
2
55
Skrtel
Martin
9
37
26
1
99
0
10
56
Agger
Daniel
9
5
26
1
99
0
4
57
Ayala
Daniel
9
40
20
1
99
0
0
58
Robinson
Jack
9
49
20
1
99
0
0
59
Degen
Philipp
9
27
27
1
99
0
2
60
Pacheco
Daniel
9
47
19
3
99
0
0
61
Agbonlahor
Gabriel
1
11
24
3
99
6
13
62
Carew
John
1
11
31
3
99
2
11
63
Cuellar
Carlos
1
24
29
1
99
0
10
64
Dunne
Richard
1
5
31
1
99
0
69
65
Friedel
Brad
1
1
39
0
99
0
2
66
Heskey
Emile
1
18
32
3
99
1
57
67
Milner
James
1
8
24
2
99
5
22
68
Petrov
Stiliyan
1
19
31
2
99
3
23
69
Warnock
Stephen
1
25
29
1
99
0
26
70
Young
Ashley
1
7
25
2
99
4
23
71
Collins
James
1
29
27
1
99
0
10
72
Downing
Stewart
1
6
26
2
99
3
8
73
Sidwell
Steve
1
4
28
2
99
3
9
74
Reo-Coker
Nigel
1
20
26
2
99
5
30
75
Young
Luke
1
2
31
1
99
0
56
76
Delfouneso
Nathan
1
14
19
3
99
6
0
77
Delph
Fabian
1
16
21
2
99
0
3
78
Beye
Habib
1
23
33
1
99
0
11
79
Albrighton
Marc
1
12
21
2
99
0
0
80
Davies
Curtis
1
15
25
1
99
0
3
81
Baines
Leighton
7
3
26
1
99
0
16
82
Cahill
Tim
7
17
31
2
99
6
40
83
Heitinga
Johnny
7
5
27
1
99
0
8
84
Howard
Tim
7
24
31
0
99
0
4
85
Saha
Louis
7
8
32
3
99
6
11
86
Bilyaletdinov
Diniyar
7
7
25
2
99
2
2
87
Distin
Sylvain
7
15
33
1
99
0
30
88
Fellaini
Marouane
7
25
23
2
99
6
18
89
Hibbert
Tony
7
2
29
1
99
0
34
90
Neville
Philip
7
18
33
1
99
0
73
91
Osman
Leon
7
21
29
2
99
6
16
92
Rodwell
Jack
7
26
19
2
99
6
3
93
Yakubu
Ayegbeni
7
22
28
3
99
5
11
94
Anichebe
Victor
7
28
22
3
99
6
8
95
Arteta
Mikel
7
10
28
2
99
3
29
96
Donovan
Landon
7
9
28
2
99
2
1
97
Gosling
Dan
7
19
20
2
99
6
1
98
Jagielka
Phil
7
6
28
1
99
0
8
99
Yobo
Joseph
7
4
30
1
99
0
10
100
Baxter
Jose
7
37
18
3
99
0
0
101
Berra
Christophe
19
16
25
1
99
0
7
102
Craddock
Jody
19
6
35
1
99
0
13
103
Doyle
Kevin
19
29
28
3
99
6
6
104
Ebanks-Blake
Sylvan
19
9
24
3
99
6
2
105
Elokobi
George
19
3
24
1
99
0
3
106
Fletcher
Steven
19
10
23
3
99
5
1
107
Foley
Kevin
19
32
26
1
99
0
1
108
Guedioura
Adlene
19
34
25
2
99
6
3
109
Hahnemann
Marcus
19
1
38
0
99
0
0
110
Halford
Greg
19
15
26
2
99
0
6
111
Henry
Karl
19
8
28
2
99
0
9
112
Jarvis
M3hew
19
17
24
2
99
5
5
113
Jones
David
19
14
26
2
99
3
8
114
Stearman
Richard
19
5
23
1
99
4
6
115
Van_Damme
Jelle
19
2
28
1
99
1
1
116
Ward
Stephen
19
11
25
1
99
0
5
117
Edwards
David
19
4
24
2
99
4
3
118
Dunleavy
John
19
36
19
1
99
0
0
119
Zubar
Ronald
19
23
25
1
99
4
4
120
Milijas
Nenad
19
20
27
2
99
5
1
121
Bent
Darren
14
11
26
3
99
6
10
122
Bramble
Titus
14
19
29
1
99
6
55
123
Elmohamady
Ahmed
14
27
23
2
99
0
0
124
Henderson
Jordan
14
10
20
2
99
2
3
125
Malbranque
Steed
14
8
30
2
99
4
30
126
Mignolet
Simon
14
22
22
0
99
0
0
127
Richardson
Kieran
14
3
26
1
99
4
34
128
Welbeck
Danny
14
17
20
3
99
6
1
129
Campbell
Fraizer
14
9
23
3
99
6
5
130
C3ermole
Lee
14
6
22
2
99
6
45
131
Onuoha
Nedum
14
15
24
1
99
5
2
132
Riveros
Cristian
14
16
28
2
99
0
0
133
Ferdinand
Anton
14
29
25
1
99
6
19
134
Bardsley
Phillip
14
2
25
1
99
0
24
135
Da_Silva
Paulo
14
14
30
1
99
0
2
136
Gyan
Asamoah
14
33
25
3
99
5
0
137
Turner
Michael
14
4
28
1
99
2
14
138
Zenden
Bolo
14
7
34
2
99
4
17
139
Angeleri
Marcos
14
12
27
1
99
0
0
140
Reid
Andy
14
20
28
2
99
3
12
141
Delap
Rory
13
24
34
2
99
6
38
142
Etherington
M3hew
13
26
29
2
99
4
14
143
Faye
Abdoulaye
13
25
32
1
99
3
34
144
Fuller
Ricardo
13
10
31
3
99
5
24
145
Huth
Robert
13
4
26
1
99
6
29
146
Sorensen
Thomas
13
29
34
0
99
0
3
147
Shawcross
Ryan
13
17
23
1
99
6
14
148
Whelan
Glenn
13
6
26
2
99
6
13
149
Whitehead
Dean
13
18
28
2
99
6
34
150
Collins
Danny
13
5
30
1
99
3
10
151
Jones
Kenwyne
13
9
26
3
99
4
6
152
Walters
Jonathan
13
19
28
3
99
0
0
153
Sanli
Tuncay
13
20
28
2
99
1
11
154
Sidibe
Mamady
13
11
31
3
99
4
1
155
Tonge
Michael
13
23
27
2
99
2
7
156
Higginbotham
Danny
13
3
32
1
99
0
17
157
Pennant
Jermaine
13
16
27
2
99
2
19
158
Pugh
Danny
13
14
28
2
99
1
0
159
Wilkinson
Andy
13
28
26
1
99
0
15
160
Wilson
Marc
13
12
23
2
99
0
3
161
Cole
Carlton
17
9
27
3
99
4
25
162
Green
Robert
17
1
30
0
99
0
2
163
Ilunga
Herita
17
23
28
1
99
0
4
164
Noble
Mark
17
16
23
2
99
2
28
165
Parker
Scott
17
8
30
2
99
3
76
166
Piquionne
Frederic
17
30
32
3
99
4
2
167
Upson
M3hew
17
15
31
1
99
4
22
168
Barrera
Pablo
17
12
23
2
99
0
1
169
Boa_Morte
Luis
17
13
33
2
99
5
65
170
Dyer
Kieron
17
7
32
2
99
2
6
171
Faubert
Julien
17
20
27
1
99
0
10
172
Gabbidon
Daniel
17
4
31
1
99
0
4
173
Reid
Winston
17
2
22
1
99
0
0
174
Behrami
Valon
17
21
25
2
99
2
4
175
Ben-Haim
Tal
17
3
28
1
99
6
30
176
Jacobsen
Lars
17
37
31
1
99
0
0
177
Kovac
Radoslav
17
14
31
2
99
4
11
178
McCarthy
Benni
17
17
33
3
99
4
17
179
Sears
Freddie
17
19
21
3
99
6
0
180
Stanislas
Junior
17
25
21
2
99
3
3
181
Alcaraz
Antolin
18
3
28
1
99
6
0
182
Boyce
Emmerson
18
17
31
1
99
6
11
183
Stam
Ronnie
18
23
26
1
99
0
0
184
Boselli
Mauro
18
9
25
3
99
0
0
185
Diame
Mohamed
18
21
23
2
99
2
7
186
Figueroa
Maynor
18
31
27
1
99
5
11
187
Gohouri
Steve
18
2
29
1
99
6
4
188
McCarthy
James
18
4
20
2
99
4
3
189
Rodallega
Hugo
18
20
25
3
99
4
6
190
Kirkland
Chris
18
1
29
0
99
0
5
191
N'Zogbia
Charles
18
10
24
2
99
6
22
192
Watson
Ben
18
8
25
2
99
6
4
193
Cleverley
Tom
18
15
21
2
99
0
0
194
Di_Santo
Franco
18
7
21
3
99
2
3
195
Gomez
Jordi
18
14
25
2
99
6
3
196
Moses
Victor
18
11
20
2
99
2
0
197
Caldwell
Gary
18
5
28
1
99
2
5
198
Caldwell
Steven
18
13
30
1
99
1
15
199
De_Ridder
Daniel
18
28
26
2
99
0
1
200
Al_Habsi
Ali
18
26
29
0
99
0
0
201
Bangura
Alhassan
4	
3
22
3
76
0
2
202
Baptiste
Alex		
4
4
24
1
69
5
0
203
Barkhuizen
Tom	
4
5
26
1
80
0
0
204
Basham
Chris	
4
6
22
3
64
5
0
205
Cathcart
Craig	
4
7
21
1
70
0
0
206
Clarke
Billy	
4
8
23
3
54
0
0
207
Coid
Danny	
4
9
29
1
69
0
0	
208
Crainey
Stephen		
4
34
29
1
74
0
0
209
Demontagnac
Ishmel	
4
65
21
3
68
0
0
210
Eardley
Neal		
4
15
22
1
69
0
0
211	
Eastham
Ashley		
4
27
18
1
76
0
0
212	
Edwards
Rob		
4
26
27
1
80
0
0
213
Euell
Jason	
4
67
33
3
81
0
0
214
Ev3
Ian	
4
21
29
1
64
0
1
215
Gilks
M3hew	
4
1
27
0
80
0
0
216	
Grandin
Elliot	
4
89
22
3
83
0
0
217	
Halstead
Mark	
4
47
20
3
79
0
0
218
Harewood
Marlon	
4
58
29
3
75
5
17
219	
Varney
Luke	
4
14
27
3
79
6
0
220
Taylor-Fletcher 
Gary	
4
30
29
3
84
2
0
221
Adebayor
Emmanuel
10
26
26
3
86
6
11
222
Barry
Gareth
10
6
29
2
84
3
64
223
Bellamy
Craig
10
10
31
3
78
6
41
224
Bridge
Wayne
10
18
29
1
77
6
13
225
De-Jong
Nigel
10
30
25
2
77
0
11
22

arrays are always, always indexed beginning from 0, never 1. So line 38 should be for (int i=0;i<401;i++) .

The index values on lines 41 and 42 should be using the loop counter i, not the value of j that was read from the file. What would your program do with a value when j == 500? (Hint: crash!) The value of j should the used as the player's ID, not the index value into the array. You should assume the player's ID has no relationship to the index value used in the array.

arrays are always, always indexed beginning from 0, never 1
...

The index values on lines 41 and 42 should be using the loop counter i, not the value of j that was read from the file.

I made the changes:

#include <fstream>

#include <iostream>

using namespace std;


int main()

{
  class plr
    {
    public:
      int id;
      int shirt_no;
      int age;
      int goals;
      int pos;//0-GK,1-DEF,2-MID,3-ATT
      char LName[51];
      char FName[51];
      int team;
      int skill;
      int goaltime;
      int cards;
      void transfer(int t_id)
      {
        team = t_id;
      }
    }plrs[400],plrs2[400];;



  fstream reader("players.txt", ios::in);
  fstream binary("players2.dat", ios::out);

  //READ FROM TEXT FILE into objects  :
int j;
  for (int i=0;i<400;i++)
    {
      reader>>j;
      plrs[i].id = j;
      reader>>plrs[i].LName>>plrs[i].FName>>plrs[i].team>>plrs[i].shirt_no>>plrs[i].age>>plrs[i].pos>>plrs[i].skill>>plrs[i].goaltime>>plrs[i].cards;


      cout << "\n\n";
    }
  reader.close();

//Display all players
for(int j=0;j<400;j++)
{cout<<"\n\n"<<plrs[j].LName<<"\n"<<plrs[j].FName<<"\n"<<plrs[j].team<<"\n"<<plrs[j].shirt_no<<"\n"<<plrs[j].age<<"\n"<<plrs[j].pos<<"\n"<<plrs[j].skill<<"\n"<<plrs[j].goaltime<<"\n"<<plrs[j].cards;
}

//Write objects to file
for (int j=0;j<400;j++){
binary.write((char*)&plrs[j], sizeof(plrs[j]));
}
  binary.close();


//Read objects from that file through another filestream into a different array of objects
  fstream plrreader("players2.dat", ios::in);
  for (int j=0;j<400;j++)
    {
      if (plrreader.read((char*)&plrs2[j], sizeof(plrs2[j])))
        {
          cout<<j<<" succesful\n";
        }


    }

  cout<<"Read successful";


//Display objects -- some error here, only 20 players seem to be readable :
  for (int j=0; j<400; j++)
    {
      cout<<"\n"<<plrs2[j].id<<"\nName :"<<plrs2[j].FName<<" "<<plrs2[j].LName<<"\nTeam :"<<plrs2[j].team<<"\n No.:"<<plrs2[j].shirt_no<<"\n Age:"<<plrs2[j].age<<"\n Pos :"<<plrs2[j].pos<<"\n Skill:"<<plrs2[j].skill<<"\n Max goals scored in"<<plrs2[j].goaltime<<"\n Cards"<<plrs2[j].cards;

    }
  plrreader.close();

  return 0;


}

But still can't figure out what is really creating the problem

Edited 6 Years Ago by anantk: n/a

A few problems:

1. There are more than 400 items in players.txt, so you need to increase the size of those two arrays

2. Add the ios::biary flag to the two binary files

3. When reading/writing binary arrays the arrays can be read/written all in one shot -- it is not necessary to write or read them one array element at a time. But sometimes it is more useful to read them one at a time if you don't know how many were written.

4. Move the class plr up above main() so that you can write other functins that use the same class. Almost no programmer will put the class definition within a function because it limits its scop to that one function. Not a very useful thing to do.

5. Instead of that array it would actually be better to use either std::vector or std::list so that you don't have to know how many items are in the files. Buth vector and list grow as you add items to them.

#include <fstream>

#include <iostream>

using namespace std;

  class plr
    {
    public:
      int id;
      int shirt_no;
      int age;
      int goals;
      int pos;//0-GK,1-DEF,2-MID,3-ATT
      char LName[51];
      char FName[51];
      int team;
      int skill;
      int goaltime;
      int cards;
      void transfer(int t_id)
      {
        team = t_id;
      }
      plr() 
      {
          id = shirt_no = age = goals = pos = 
              team = skill = goaltime = cards = 0;
          LName[0] = 0;
          FName[0] = 0;
      }
  };

int main()

{
    plr plrs[402];
    plr plrs2[402];



    ifstream reader("players.txt", ios::in);
    ofstream binary("players2.dat", ios::out | ios::binary);

    //READ FROM TEXT FILE into objects  :
    int i = 0, j = 0;
    while( reader >> j )
    {
      plrs[i].id;
      reader>>plrs[i].LName>>plrs[i].FName>>plrs[i].team>>plrs[i].shirt_no>>plrs[i].age>>plrs[i].pos>>plrs[i].skill>>plrs[i].goaltime>>plrs[i].cards;
      ++i;

    }
    cout << "i = " << i << '\n';
    reader.close();

    //Display all players
    for(int j=0;j<i;j++)
    {
        cout<<"\n\n"<<plrs[j].LName<<"\n"<<plrs[j].FName<<"\n"<<plrs[j].team<<"\n"<<plrs[j].shirt_no<<"\n"<<plrs[j].age<<"\n"<<plrs[j].pos<<"\n"<<plrs[j].skill<<"\n"<<plrs[j].goaltime<<"\n"<<plrs[j].cards;
    }

  binary.write((char *)plrs, sizeof(plrs));
  binary.close();


//Read objects from that file through another filestream into a different array of objects
  ifstream plrreader("players2.dat", ios::in | ios::binary);
  plrreader.read((char*)&plrs2, sizeof(plrs2));
  plrreader.close();
  int nRows = (int)plrreader.gcount() / sizeof(plr);
//Display objects -- some error here, only 20 players seem to be readable :
  for (int j = 0; j < nRows; j++)
   {
      cout<<"j: " << j << ": " <<plrs2[j].id<<"Name :"<<plrs2[j].FName<<" "<<plrs2[j].LName
          <<" Team :"<<plrs2[j].team<<" No.:"<<plrs2[j].shirt_no<<" Age:"<<plrs2[j].age<<" Pos :"
          <<plrs2[j].pos<<" Skill: "<<plrs2[j].skill<<" Max goals scored in "
          <<plrs2[j].goaltime<<" Cards "<<plrs2[j].cards << '\n';
    }

  return 0;


}

Edited 6 Years Ago by Ancient Dragon: n/a

Comments
thanks

Thanks, the program works now.

Yes, there seem to be 401 players in players.txt .. Will check the file later

I tried the code, but all player ids are '0' (as initialized before)
In the first loop

while( reader >> j )
{
plrs[i].id;
..

shouldn't it be reader>>plrs[i].id ?
I tried reader>>plrs[i].id , but it fails to read any player..
Am I missing something?

(As a quick fix I've changed it to plrs[i].id=i+1; )

should have been plrs.id = j;

>>As a quick fix I've changed it to plrs.id=i+1;
Well you changed it wrong. Your original code snippet in your first post was correct for that line.

This question has already been answered. Start a new discussion instead.