fstream Newbie Help!

Pages: 12
Using xCode, FWIW.
Basically, didn't really get this concept in class, and cannot figure it out by searching online so far. Here's my assignment:
"You are to write a program that will process a text file that contains the following information for a group of professional baseball players (one line of data per player): Name (at most 20 characters, padded with blanks on the right), At Bats, Hits, Walks, Hit by Pitches, and Sacrifice Flies. Sample data for one player: Francisco Cervelli 326 86 56 6 5 This means that in the year this data was gathered (2016), Francisco Cervelli had 326 at bats, 86 hits, 56 walks, 6 hit by pitches, and 5 sacrifice flies. Thus, his on-base percentage for the season was: = 0.3766 . You should prompt the user for the name of an input data file, make sure the requested file exists, prompt the user for the name of an output file, make sure it does not already exist, and then, for each player: -- read in the data -- calculate the on-base percentage -- display the on-base percentage along with the player’s name -- store the player’s name, on-base percentage, and number of at bats in the output file (one line per player). After all players have been processed, your program should display the following and also store this information in the output file: -- Player with the highest on-base percentage and this OBP. -- Player with the lowest on-base percentage and this OBP. -- The average on-base percentage for all players, calculated in each of the two ways discussed in class. Use at least a few functions (with complete, proper argument lists -- do NOT use global variables) in this program. Your code should be fully documented. A portion of your grade on this project will depend on your program structure and organization."

I'm stuck on trying to get the data from a text file, because I can't seem to reference a pre-created one, or create one with an ofstream. Here's the code I've tried. Not really sure where I'm going wrong, but I know it's probably a simple fix. Thanks, guys!


// // main.cpp // Baseball Statistics //
include <iostream>

include <string>

include <fstream>

using namespace std;
int main() {
/*
ofstream Stats;
Stats.open("Stats.file");
Stats << "Josh Donaldson " << 36 << 15 << 3 << 0 << 0 << "\n";
Stats << "Brock Holt " << 10 << 4 << 0 << 0 << 0 << "\n";
Stats << "Elvis Andrus " << 11 << 4 << 0 << 0 << 0 << "\n";
Stats << "Edwin Encarnacion " << 35 << 10 << 4 << 0 << 0 << "\n";
Stats << "Francisco Lindor " << 58 << 18 << 4 << 0 << 0 << "\n";
Stats << "Ezequiel Carrera " << 33 << 10 << 2 << 0 << 0 << "\n";
Stats << "Rougned Odor " << 10 << 2 << 2 << 0 << 0 << "\n";
Stats << "Mookie Betts " << 10 << 2 << 2 << 0 << 0 << "\n";
Stats << "Carlos Santana " << 52 << 10 << 8 << 0 << 0 << "\n";
Stats << "Jose Bautista " << 33 << 6 << 6 << 0 << 0 << "\n";
Stats.close();

*/

ifstream Stats;
Stats.open("/Users/chris/Documents/Stats.txt");
Stats.fail();

char name [21];
Stats.get (name,21);

int ab,h,bb,hbp,sf;

while (!eof)
Stats >> name >> ab >> h >> bb >> hbp >> sf >> ws;




return 0;
}
What are the contents of the file "/Users/chris/Documents/Stats.txt"?
They're supposed to be the same as what is commented out, since the commented out code didn't work. But that didn't work either. We're supposed to use the ofstream file anyways.
1
2
3
4
5
6
7
8
9
10
Stats << "Josh Donaldson " << 36 << 15 << 3 << 0 << 0 << "\n";
Stats << "Brock Holt " << 10 << 4 << 0 << 0 << 0 << "\n";
Stats << "Elvis Andrus " << 11 << 4 << 0 << 0 << 0 << "\n";
Stats << "Edwin Encarnacion " << 35 << 10 << 4 << 0 << 0 << "\n";
Stats << "Francisco Lindor " << 58 << 18 << 4 << 0 << 0 << "\n";
Stats << "Ezequiel Carrera " << 33 << 10 << 2 << 0 << 0 << "\n";
Stats << "Rougned Odor " << 10 << 2 << 2 << 0 << 0 << "\n";
Stats << "Mookie Betts " << 10 << 2 << 2 << 0 << 0 << "\n";
Stats << "Carlos Santana " << 52 << 10 << 8 << 0 << 0 << "\n";
Stats << "Jose Bautista " << 33 << 6 << 6 << 0 << 0 << "\n";


It should be :
1
2
3
4
5
6
7
8
9
10
Stats << "Josh Donaldson " << 36 << ' ' << 15 << ' ' << 3 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Brock Holt " << 10 <<  ' ' << 4 <<  ' ' << 0 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Elvis Andrus " << 11 <<  ' ' << 4 <<  ' ' << 0 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << 35 <<  ' ' << 10 <<  ' ' << 4 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Francisco Lindor " << 58 <<  ' ' << 18 <<  ' ' << 4 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << 33 <<  ' ' << 10 <<  ' ' << 2 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Rougned Odor " << 10 <<  ' ' << 2 <<  ' ' << 2 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Mookie Betts " << 10 <<  ' ' << 2 <<  ' ' << 2 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Carlos Santana " << 52 <<  ' ' << 10 <<  ' ' << 8 <<  ' ' << 0 <<  ' ' << 0 << "\n";
Stats << "Jose Bautista " << 33 <<  ' ' << 6 <<  ' ' << 6 <<  ' ' << 0 <<  ' ' << 0 << "\n";
When I get rid of the comments, and reference Stats.text, like below, I get a bunch of errors. I'm just trying to figure out how to create that output file, read it, and put the info into variables.

ofstream Stats;
Stats.open("Stats.txt");
Stats << "Josh Donaldson " << 36 << ' ' << 15 << ' ' << 3 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Brock Holt " << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Elvis Andrus " << 11 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << 35 << ' ' << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Francisco Lindor " << 58 << ' ' << 18 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << 33 << ' ' << 10 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Rougned Odor " << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Mookie Betts " << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Carlos Santana " << 52 << ' ' << 10 << ' ' << 8 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Jose Bautista " << 33 << ' ' << 6 << ' ' << 6 << ' ' << 0 << ' ' << 0 << "\n";
Stats.close();



ifstream Stats;
Stats.open("Stats.txt");
Stats.fail();

char name [21];
Stats.get (name,21);

int ab,h,bb,hbp,sf;

while (!eof)
Stats >> name >> ab >> h >> bb >> hbp >> sf >> ws;
1
2
3
4
5
6
ifstream Stats;
Stats.open("Stats.txt");
Stats.fail();

char name [21];
Stats.get (name,21);


It should be :
1
2
3
4
5
6
ifstream Stats2;
Stats2.open("Stats2.txt");
Stats2.fail();

char name [21];
Stats2.get (name,21);


You cannot have duplicate variables (names) in the same function.
Last edited on
So how do I extract the data from Stats.txt?
So how do I extract the data from Stats.txt?

What do you mean?
I need to be able to store all of the players' names in variables in my cpp file, along with their at-bats, hit, walks, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
ofstream Stats;
Stats.open("Stats.txt");
Stats << "Josh Donaldson " << 36 << ' ' << 15 << ' ' << 3 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Brock Holt " << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Elvis Andrus " << 11 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << 35 << ' ' << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Francisco Lindor " << 58 << ' ' << 18 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << 33 << ' ' << 10 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Rougned Odor " << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Mookie Betts " << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Carlos Santana " << 52 << ' ' << 10 << ' ' << 8 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Jose Bautista " << 33 << ' ' << 6 << ' ' << 6 << ' ' << 0 << ' ' << 0 << "\n";
Stats.close();


The following already extracts the data into a file. What do you need, I do not seem to understand your question.
The ofstream should create a new file called "Stats.txt". I want to be able to "get" (.get()) the info from this new file, and put it in variables in the cpp file.

Basically, when I type out this:

char name [21];
Stats.get(name, 21);

Why does that not either output "Josh Donaldson", or at least assign "Josh Donaldson" to "name"?

I basically don't know how to read text files in C++.
ifstream is for reading files, ofstream is for writing files.
Is your question about ifstream or ofstream?
Last edited on
Well, my assignment in the OP says I need both. But I'm really more concerned about ifstream.

My assignment wants me to write a file, and then read it.
Then you write into the file like the following.
1
2
3
4
5
6
7
8
9
10
11
12
13
ofstream Stats;
Stats.open("Stats.txt");
Stats << "Josh Donaldson " << endl << 36 << ' ' << 15 << ' ' << 3 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Brock Holt " << endl << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Elvis Andrus " << endl << 11 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << endl << 35 << ' ' << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Francisco Lindor " << endl << 58 << ' ' << 18 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << endl << 33 << ' ' << 10 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Rougned Odor " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Mookie Betts " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Carlos Santana " << endl << 52 << ' ' << 10 << ' ' << 8 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Jose Bautista " << endl << 33 << ' ' << 6 << ' ' << 6 << ' ' << 0 << ' ' << 0 << "\n";
Stats.close();

Post the contents of your output file, and let me know so that we can continue.
It doesn't seem to be creating an output file. The code I have should be, but it's not.
The you try the following :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ofstream Stats;
Stats.open("Stats.txt");
Stats << "Josh Donaldson " << endl << 36 << ' ' << 15 << ' ' << 3 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Brock Holt " << endl << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Elvis Andrus " << endl << 11 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << endl << 35 << ' ' << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Francisco Lindor " << endl << 58 << ' ' << 18 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << endl << 33 << ' ' << 10 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Rougned Odor " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Mookie Betts " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Carlos Santana " << endl << 52 << ' ' << 10 << ' ' << 8 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Jose Bautista " << endl << 33 << ' ' << 6 << ' ' << 6 << ' ' << 0 << ' ' << 0 << "\n";
Stats.close();
system("Stats.txt");


Be sure to include the header <cstdlib>.
Hmm, still can't see the file. It should be created within my Xcode project, no?

Can't find the file anywhere on my Mac.
Last edited on
Then you try the following :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ofstream Stats;
Stats.open("Stats.txt");
Stats << "Josh Donaldson " << endl << 36 << ' ' << 15 << ' ' << 3 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Brock Holt " << endl << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Elvis Andrus " << endl << 11 << ' ' << 4 << ' ' << 0 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Edwin Encarnacion " << endl << 35 << ' ' << 10 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Francisco Lindor " << endl << 58 << ' ' << 18 << ' ' << 4 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Ezequiel Carrera " << endl << 33 << ' ' << 10 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Rougned Odor " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Mookie Betts " << endl << 10 << ' ' << 2 << ' ' << 2 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Carlos Santana " << endl << 52 << ' ' << 10 << ' ' << 8 << ' ' << 0 << ' ' << 0 << "\n";
Stats << "Jose Bautista " << endl << 33 << ' ' << 6 << ' ' << 6 << ' ' << 0 << ' ' << 0 << "\n";
Stats.close();

string line;
cout << "Reading the file : " << endl;
ifstream inFile("Stats.txt");
while(getline(inFile, line)) cout << line << endl;
inFile.close();

And show us the contents of the file.
That gave me the contents of the file. Now, how would I do it one-by-one? I want to extract the first player's name, then their OBP, then AB, Hits, Walks, etc. Then do the same for the second/third/fourth/etc. player.

I don't want to get the whole line at once, but I want to get the first string (the name), assign that to a variable, and do the same for the numbers.

Anyways, here's what that got me (and I don't know why it worked, or how):

Reading the file :
Josh Donaldson 36 15 3 0 0
Brock Holt 10 4 0 0 0
Elvis Andrus 11 4 0 0 0
Edwin Encarnacion 35 10 4 0 0
Francisco Lindor 58 18 4 0 0
Ezequiel Carrera 33 10 2 0 0
Rougned Odor 10 2 2 0 0
Mookie Betts 10 2 2 0 0
Carlos Santana 52 10 8 0 0
Jose Bautista 33 6 6 0 0
Last edited on
The output should actually be :
Josh Donaldson 
36 15 3 0 0
Brock Holt 
10 4 0 0 0
Elvis Andrus 
11 4 0 0 0
Edwin Encarnacion 
35 10 4 0 0
Francisco Lindor 
58 18 4 0 0
Ezequiel Carrera 
33 10 2 0 0
Rougned Odor 
10 2 2 0 0
Mookie Betts 
10 2 2 0 0
Carlos Santana 
52 10 8 0 0
Jose Bautista 
33 6 6 0 0 


Is it correct?
Pages: 12