Input The input for program 1 will be read from a file called teams.txt. The file consists of a set of records for the teams that competed in the tournament. Each record represents one team and is made up of multiple fields that are separated by colons. The fields for each team are: the school name, the team name, the score for game 1, the score for game 2, and the score for game 3. A record in the file resembles the following: DELAWARE ST.:BLUE HENS:187:162:171 Output The output for program 1 will be a file called results.txt, which will contain a structure for each of the teams in the teams.txt file. Suggested Logic for main() Create a teamInfo variable, C-Style string, input file stream, and output file stream. Open the file teams.txt for input and verify that it opened correctly Open the file results.txt for binary output and verify that it opened correctly Use getline to read the first team from the input file into the C-Style string While there are input records in the input file Call the makeTeam function to break the C-Style string into the various fields for the structure Write the structure to the output file using the write command Use getline to read the next team from the input file into the C-Style string Endwhile Close the input and output files main note: A structure can be written to a file by using the write function. For example: teamInfo aTeam; outFile.write( (char *) &aTeam, sizeof(aTeam) ); Function to write and use void makeTeam( char *inputLine, teamInfo &aTeam ) This function will take the information in inputLine and break it into the 5 individual fields for a team and use them to build a teamInfo structure. It takes 2 arguments: a C-Style string that contains team information and a reference to a teamInfo structure that can be used to pass back a team. As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to search for a colon (':') so that each field can be isolated, strcpy (or atoi or atof ) can then be used to put the isolated string into the appropriate field of the structure. The atoi and atof functions will be used to convert a string to its numeric representation (atoi for converting to integer and atof for converting to float/double). |
#include <iostream> #include <iomanip> #include <cstring> #include <fstream> using namespace std; struct teamInfo { char schoolName[20]; char teamName[20]; int game1score; int game2score; int game3score; }; void makeTeam(char *, teamInfo&); int main() { teamInfo aTeam; char inputLine[80]; ifstream inFile; ofstream outFile; inFile.open("teams.txt"); outFile.open("results.txt", ios::binary); if (inFile.fail()) { cout << "Failed to open"; exit(1); } if (outFile.fail()) { cout << "Failed to write"; exit(1); } while(inFile.getline(inputLine, 80)) { makeTeam(inputLine, aTeam); } inFile.close(); outFile.close(); system("pause"); return 0; } void makeTeam(char *inputLine, teamInfo &aTeam) { } |
|
|
while(inFile.getline(inputLine, 80)) { makeTeam(inputLine, aTeam); } inFile.close(); outFile.close(); system("pause"); return 0; } void makeTeam(char *inputLine, teamInfo &aTeam) { int i = 0; int ptr; while(inputLine[i] != ':') { aTeam.schoolName[i] = inputLine[i]; i++; } while(inputLine[i] != ':') { aTeam.teamName[i] = inputLine[i]; i++; } cout << endl << aTeam.schoolName << endl << aTeam.teamName; } |
void makeTeam(char *inputLine, teamInfo &aTeam) { int i = 0; while(inputLine) { if(inputLine[i] == ':') { i++; break; } else { aTeam.schoolName[i] = inputLine[i]; i++; } } while(inputLine) { if(inputLine[i] == ':') { i++; break; } else { aTeam.teamName[i] = inputLine[i]; i++; } } cout << endl << aTeam.schoolName << endl << aTeam.teamName; } |
|
|
|
|