### 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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142`` ``````#include #include #include #include #include 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<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

This is the binary_hockey file coding (a .cpp and .txt file combined to be inputted into the previous code)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586`` ``````#include #include #include #include #include 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
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
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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586`` ``````#include #include #include #include #include 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205`` ``````#include #include #include #include #include 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<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
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:
 ``12345678`` ``````int main(void) { std::ofstream out(data.file, std::ios::binary); int x = 0x41424344; out.write(reinterpret_cast(&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.