Inputting Binary into a struc

My code works fine, outside of the input file. I'm trying to input the file binary_hockey into my program, which contains hockey stats that'll be ordered into 2 lists and also prints out the average goals and assists, plus the leading scorer and assists.

This is the only trouble area, any help would be appreciated. Thank you!

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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
 
using namespace std;

const int array_size=35;
 
struct playerInfo
{
char playerName[25];
int goalsScored;
int assist;
};
 
// Setting up all prototypes
int buildArrays( playerInfo[]);
void printArrays( playerInfo[],int n );
void sortArrays( playerInfo[],int n);
void calcStats(playerInfo[],int n,double *ag,double *aa);
 
int main()
{
double averageGoals,averageAssists;
playerInfo player[array_size];

int n=buildArrays(player);
cout << "Chicago Blackhawks UNSORTED Report" << endl;
cout << endl;
printArrays(player,n);
sortArrays(player,n);
cout<<"\nChicago Blackhawks SORTED Report";
cout << endl;
printArrays(player,n);
calcStats(player,n,&averageGoals,&averageAssists);
 
cout << endl << "Blackhawks Statistics" << endl;
cout << "---------------------" << endl;
 
cout<<endl<<"Average Number of Goals Scored "<<averageGoals<<endl;
cout<<"Average Number of Assists "<<averageAssists<<endl;
system ("pause");
return 0;
}
 
// Setting up all functions
 
/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill in the structure array. It
takes as its arguments the array of playerInfo.
It returns the number of valid players that were placed in the arrays.
*****************************************************************************/
 
int buildArrays(playerInfo player[])
{
std::ifstream inFile;
inFile.open( "binary_hockey", ios::binary );
if( inFile.fail() )
{
cout << "The binary_hockey input file did not open";
system ("pause");
exit(-1);
}
int i=0;
int Player, onePlayer;
while( inFile )
{
Player, onePlayer;
inFile.read( (char *) &onePlayer, sizeof(Player) );
}
inFile.close();
return i-1;
}
 
 
/*****************************************************************************
void printArrays( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments structure playerInfo arrays and
the number of players in the in player array.
*****************************************************************************/
 
void printArrays( playerInfo player[], int numPlayers )
{
cout<<"Player\t\t\tGoals\tAssists\tPoints"<<endl;
cout<<"----------------------------------------"<<endl;
 for(int i=0;i<numPlayers;i++)
        cout<<player[i].playerName<<"\t\t"<<player[i].goalsScored<<"\t"<<player[i].assist<<"\t"<<player[i].goalsScored+player[i].assist<<endl;
cout<<"----------------------------------------"<<endl;
}
 
/*****************************************************************************
void sortArrays( )
This function will sort the arrays in DESCENDING order based on the number
of goals. Use the selection sort algorithm presented in lecture. This function
takes as its arguments playerInfo array and the number of players in the arrays.
*****************************************************************************/
 
void sortArrays( playerInfo player[], int numPlayers )
{
playerInfo temp;
for(int i=0;i<numPlayers-1;i++)
{
       for(int j=i+1;j<numPlayers;j++)
       {            
             
              if(strcmp(player[i].playerName,player[j].playerName)>0)
              {
                     temp=player[i];
                     player[i]=player[j];
                     player[j]=temp;
              }
       }
}
}
 
/*****************************************************************************
void calcStats( )
This function will calculate average number of goals and average number of assists.
This function takes as its arguments playerInfo array and the number of
players in the arrays,reference of average goals and reference of average assists.
*****************************************************************************/
 
 
void calcStats(playerInfo player[],int numPlayers,double *ag,double *aa)
{
      
       int totalGoals=0,totalAssists=0;
       for(int i=0;i<numPlayers;i++)
       {
              totalGoals +=player[i].goalsScored;
              totalAssists +=player[i].assist;
       }
       *ag=totalGoals/numPlayers;
       *aa=totalAssists/numPlayers;
      
}


This is the binary_hockey file coding (a .cpp and .txt file combined to be inputted into the previous 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
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
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

//The structure that is used to represent one player

struct Player
{
char name[20];
int goals;
int assists;
int rating;
};

int main()
{
Player onePlayer;          //holds the player information from the file

ifstream infile;           //input file
ofstream outfile;          //output file

char inputVal[80];         //string for reading data from the file


//Open the input and output files and verify that they opened correctly

infile.open( "hockey2.txt" );
if( infile.fail() )
  {
  cout << "hockey2.txt failed to open";
  exit(-1);
  }

outfile.open( "binary_hockey", ios::binary );
if( outfile.fail() )
  {
  cout << "binary_hockey failed to open";
  exit(-1);
  }


//Get the first player name from the input file

infile.getline(inputVal,80);


//While there is player information in the input file
    
while( infile )
  {
  //Fill in the structure one part at a time. The data is read as a string
  //and converted to an integer value if it's numeric

  strcpy( onePlayer.name, inputVal );

  infile.getline(inputVal,80);
  onePlayer.goals = atoi( inputVal );

  infile.getline(inputVal,80);
  onePlayer.assists = atoi( inputVal );

  infile.getline(inputVal,80);
  onePlayer.rating = atoi( inputVal );

  //Write the complete structure to the output file

  outfile.write( (char *) &onePlayer, sizeof( onePlayer ) );

  //Get the next player name from the input file

  infile.getline(inputVal,80);
  }


//Close the input and output files

infile.close();
outfile.close();

return 0;
}


hoeckey2.txt file
Bryan Bickell
2
5
+2
Brandon Bolig
0
0
0
Dave Bolland
4
2
-1
Sheldon Brookbank
0
0
-1
Daniel Carcillo
0
1
+3
Michael Frolik
1
1
0
Niklas Hjalmarsson
0
2
+6
Marian Hossa
6
6
+6
Patrick Kane
9
10
+7
Duncan Keith
1
4
0
Marcus Kruger
2
2
-1
Nick Leddy
1
4
+4
Jamal Mayers
0
0
+1
Johnny Oduya
0
3
+9
Michal Rozsival
0
3
+4
Brandon Saad
1
0
+2
Brent Seabrook
1
4
+1
Patrick Sharp
2
9
+3
Andrew Shaw
2
3
+3
Viktor Stalberg
3
3
+2
Jonathon Toews
6
6
+9
Last edited on
Anyone? I'm having the same issues myself.
Did some minor editing of the code, basically made it cleaner to read. Binary is still giving me issues after trying out about 3 different approaches -_-
Whats the problem?
The binary file is not being read into the program (won't print out anything). This is the only problem I'm having
There is nothing wrong with the code that I can see. It also compiles fine.

The binary file is not being read into the program (won't print out anything). This is the only problem I'm having


Maybe there is some misunderstanding here. The binary file is not the file that is being read; the text file is. The code doesn't say anything about printing anything to the console. All it's supposed to do is read the data from the text file, and then store it in the binary file.
Ohhhhhhhhh

So for it to print out something to the console, would it need to be in .txt form or do I need to prompt the program to print out the info?
I'm guessing that you're not the one who wrote the code...

The first piece of code you have posted, the one that is 142 lines long, reads a binary file called "binary_hockey" and prints all the stats to the console.

The second piece of code you have posted, the one that is 86 lines long, reads a text file called "hockey2.txt" and writes all that information into a binary file called "binary_hockey".

So just run the second piece of code before running the first piece of code.

EDIT:

Just noticed that the struct used in the two pieces of code are different. You'll need to edit the code so that they're the same for the two pieces to work properly together.
Last edited on
Half of it is code not written by me, but I've re-edited to merge it with coding I wrote. I think that's what's causing all the issues is a small mistake with merging two different codes together.

The strucs are now the same but I'm still getting nothing. Gonna read through everything to make sure everything reads correctly.

This is the redone binary_hockey 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

//The structure that is used to represent one player

struct playerInfo
{
char name[20];
int goals;
int assists;
int rating;
};

int main()
{
playerInfo onePlayer;          //holds the player information from the file

ifstream infile;           //input file
ofstream outfile;          //output file

char inputVal[80];         //string for reading data from the file


//Open the input and output files and verify that they opened correctly

infile.open( "hockey2.txt" );
if( infile.fail() )
  {
  cout << "hockey2.txt failed to open";
  exit(-1);
  }

outfile.open( "binary_hockey", ios::binary );
if( outfile.fail() )
  {
  cout << "binary_hockey failed to open";
  system ("pause");
  exit(-1);
  }


//Get the first player name from the input file

infile.getline(inputVal,80);


//While there is player information in the input file
    
while( infile )
  {
  //Fill in the structure one part at a time. The data is read as a string
  //and converted to an integer value if it's numeric

  strcpy( onePlayer.name, inputVal );

  infile.getline(inputVal,80);
  onePlayer.goals = atoi( inputVal );

  infile.getline(inputVal,80);
  onePlayer.assists = atoi( inputVal );

  infile.getline(inputVal,80);
  onePlayer.rating = atoi( inputVal );

  //Write the complete structure to the output file

  outfile.write( (char *) &onePlayer, sizeof( onePlayer ) );

  //Get the next player name from the input file

  infile.getline(inputVal,80);
  }


//Close the input and output files

infile.close();
outfile.close();

return 0;
}


For future notice, is it easier for C++ to read .txt files?
Last edited on
I went and merged both pieces of code together for you. Had to make some changes. As I said, all the code has to be changed so that they use the same struct. Also, the buildArrays function was completely broken and incomplete. Other than that, I didn't change anything. I tried it and it worked.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

//The structure that is used to represent one player

struct playerInfo
{
    char playerName[25];
    int goalsScored;
    int assist;
    int rating;
};

const int array_size=35;
 
// Setting up all prototypes
void TextToBinary();
int buildArrays( playerInfo[]);
void printArrays( playerInfo[],int n );
void sortArrays( playerInfo[],int n);
void calcStats(playerInfo[],int n,double *ag,double *aa);
 
int main()
{
    TextToBinary();
    double averageGoals,averageAssists;
    playerInfo player[array_size];

    int n=buildArrays(player);
    cout << "Chicago Blackhawks UNSORTED Report" << endl;
    cout << endl;
    printArrays(player,n);
    sortArrays(player,n);
    cout<<"\nChicago Blackhawks SORTED Report";
    cout << endl;
    printArrays(player,n);
    calcStats(player,n,&averageGoals,&averageAssists);
     
    cout << endl << "Blackhawks Statistics" << endl;
    cout << "---------------------" << endl;
     
    cout<<endl<<"Average Number of Goals Scored "<<averageGoals<<endl;
    cout<<"Average Number of Assists "<<averageAssists<<endl;
    system ("pause");
    return 0;
}
 
// Setting up all functions
 
/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill in the structure array. It
takes as its arguments the array of playerInfo.
It returns the number of valid players that were placed in the arrays.
*****************************************************************************/
 
int buildArrays(playerInfo player[])
{
    std::ifstream inFile;
    inFile.open( "binary_hockey", ios::binary );
    if( inFile.fail() )
    {
        cout << "The binary_hockey input file did not open";
        system ("pause");
        exit(-1);
    }
    playerInfo onePlayer;
    int i;
    for (i = 0; inFile.read( (char *) &onePlayer, sizeof(playerInfo) ); i++)
    {
        player[i] = onePlayer;
    }
    inFile.close();
    return i;
}
 
 
/*****************************************************************************
void printArrays( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments structure playerInfo arrays and
the number of players in the in player array.
*****************************************************************************/
 
void printArrays( playerInfo player[], int numPlayers )
{
    cout<<"Player\t\t\tGoals\tAssists\tPoints"<<endl;
    cout<<"----------------------------------------"<<endl;
    for(int i=0;i<numPlayers;i++)
        cout<<player[i].playerName<<"\t\t"<<player[i].goalsScored<<"\t"
		    <<player[i].assist<<"\t"<<player[i].goalsScored+player[i].assist<<endl;
    cout<<"----------------------------------------"<<endl;
}
 
/*****************************************************************************
void sortArrays( )
This function will sort the arrays in DESCENDING order based on the number
of goals. Use the selection sort algorithm presented in lecture. This function
takes as its arguments playerInfo array and the number of players in the arrays.
*****************************************************************************/
 
void sortArrays( playerInfo player[], int numPlayers )
{
    playerInfo temp;
    for(int i=0;i<numPlayers-1;i++)
    {
        for(int j=i+1;j<numPlayers;j++)
        {            
            if(strcmp(player[i].playerName,player[j].playerName)>0)
            {
                temp=player[i];
                player[i]=player[j];
                player[j]=temp;
            }
        }
    }
}
 
/*****************************************************************************
void calcStats( )
This function will calculate average number of goals and average number of assists.
This function takes as its arguments playerInfo array and the number of
players in the arrays,reference of average goals and reference of average assists.
*****************************************************************************/
 
 
void calcStats(playerInfo player[],int numPlayers,double *ag,double *aa)
{
    int totalGoals=0,totalAssists=0;
    for(int i=0;i<numPlayers;i++)
    {
        totalGoals +=player[i].goalsScored;
        totalAssists +=player[i].assist;
    }
    *ag=totalGoals/numPlayers;
    *aa=totalAssists/numPlayers;
}

void TextToBinary()
{
    playerInfo onePlayer;          //holds the player information from the file

    ifstream infile;           //input file
    ofstream outfile;          //output file

    char inputVal[80];         //string for reading data from the file

    //Open the input and output files and verify that they opened correctly

    infile.open( "hockey2.txt" );
    if( infile.fail() )
    {
        cout << "hockey2.txt failed to open";
        exit(-1);
    }

    outfile.open( "binary_hockey", ios::binary );
    if( outfile.fail() )
    {
        cout << "binary_hockey failed to open";
        exit(-1);
    }

    //Get the first player name from the input file

    infile.getline(inputVal,80);

    //While there is player information in the input file

    while( infile )
    {
        //Fill in the structure one part at a time. The data is read as a string
        //and converted to an integer value if it's numeric

        strcpy( onePlayer.playerName, inputVal );

        infile.getline(inputVal,80);
        onePlayer.goalsScored = atoi( inputVal );

        infile.getline(inputVal,80);
        onePlayer.assist = atoi( inputVal );

        infile.getline(inputVal,80);
        onePlayer.rating = atoi( inputVal );

        //Write the complete structure to the output file

        outfile.write( (char *) &onePlayer, sizeof( onePlayer ) );

        //Get the next player name from the input file

        infile.getline(inputVal,80);
    }

    //Close the input and output files

    infile.close();
    outfile.close();
}
Binary files are easier for the computer (c++) to read. The downside being that you can't easily edit a binary file (compared to text).

Whenever you have problems with a certain part/idea of a program, I would recommend starting a new project with just the problem in mind. Its easier to see why this wouldnt print to console:
1
2
3
4
5
6
7
8
int main(void)
{
  std::ofstream out(data.file, std::ios::binary);
  int x = 0x41424344;
  out.write(reinterpret_cast<char*>(&x), sizeof(x));
  out.close();
  return 0;
}

~It also helps to test with output you can understand. If you open data.file in a text editor you will see ABCD or DBCA.
Last edited on
Well thank you everyone for your help, and the clarification between .txt and binary. I'll make sure to make a note of your post LowestOne for future programs I write

Thank you again!
Topic archived. No new replies allowed.