Advertisement
andrefecto

opening a file

Dec 7th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void openFile(ifstream& input)
  10. {
  11.         string file = "superbowl.dat"; // declare a variable and set it equal to the name of the superbowl file
  12.         input.open( file.c_str() ); // next open that file. doing file.c_str() tells the program to superbowl.dat in                             for file and then open that file.
  13. }
  14.  
  15.  
  16. void takeIn(ifstream& input, string winningTeam[], string losingTeam[], int finalScore[]) //made 3 arrays here that store, winning team, losing team, and final score. they are declared down in main. this is to make it easier to pass these arrays by reference later in the program.
  17. {
  18.         for(int i = 0; i < 47; i++) // loops so that you can take in all of the games listed in the open file
  19.         {
  20.                 input >> winningTeam[i]; //takes in the winning team
  21.                 input >> losingTeam[i]; // takes in the losing team
  22.                 input >> finalScore[i]; // takes in the score
  23.         }
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29.     string winningTeam[48] = { };
  30.     string losingTeam[48] = { };
  31.     int finalScore[48] = { };
  32.     ifstream input; // This says that we will now refer to our ifstream as input. this is important for the void openFile function
  33.  
  34.     openFile(input); // this runs the openFile function
  35.     takeIn(input, winningTeam, losingTeam, finalScore); // this runs the takeIn function
  36.     input.close(); // this closes the file stream
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement