Hey guys. I have a two part homework assignment. It involves reading a text file, creating a structure, and writing the results into a new text file.
I have completed the first program and according to my TA it is 100%. My issue is I ran into a mental block on this 2nd program and I just really can't figure out what to do.
Homeworkhttp://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10.htm
My 1st program:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;
struct playerinfo
{
char playername[25];
int goalsscored;
int assists;
int shots;
};
void makeplayer (char *inputline, playerinfo &player);
int main()
{
ifstream inputfile; //the file we read from
inputfile.open( "players.txt" );
if( inputfile.fail() ) //error checking
{
cout << "players.txt failed to open";
exit(-1);
}
ofstream outputfile; //the file to write to
outputfile.open( "team.txt", ios::binary );
if( inputfile.fail() ) //error checking
{
cout << "team.txt failed to open";
exit(-1);
}
playerinfo player;
char line[81]; //length of line of text
inputfile.getline (line,80);
while (inputfile) //reads each line of the file
{
makeplayer (line,player);
outputfile.write( (char *) &player, sizeof(player) );
inputfile.getline (line,80); //returns back to structure
}
inputfile.close ();
outputfile.close ();
return 0;
}
void makeplayer (char *inputline, playerinfo &player) //make char function
{
char *s1; //pointers used to read the lines of text in the file
char *s2;
char *s3;
s1 = strchr(inputline,':'); //finds the first ":"
*s1 = '\0';
strcpy(player.playername,inputline); //copy the line
s2 = strchr(s1+1,':'); //finds the second ":"
*s2 = '\0'; //null
player.goalsscored = atoi(s1+1); //converts string to a numerical format
s3 = strchr (s2+1,':'); //finds the third ":'
*s3 = '\0'; //null
player.assists = atoi(s2+1);
player.shots = atoi(s3+1); //finds the 4th and final segment of information in the line of text
}
My 2nd program that I need help with:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;
int buildArray (playerinfo, playerar[]); //array that will be used to organize the new information in teams.txt
int main()
{
ifstream inputfile; //the file we read from
inputfile.open( "team.txt", ios::binary ); //opens the new file we just created in the previous program
if( inputfile.fail() )
{
cout << "team.txt failed to open"; //error checking
exit(-1);
}
}