Ifstream failure

Hi guys!

I'm trying to do the last part of my assignment and the code works fine, but I have an issue. When I read from a file (that the user inputs the name of), it keeps reading the same line over and over again.

Text file:

5
TT
TT
1
1
1
TS
TS
2
2
2
GG
GG
3
3
3
GR
GR
4
4
4
GZ
GZ
5
5
5


Function in main
1
2
3
4
5
6
7
8
9
10
11
void readPlayersFromFile(Team &myTeam)
{

	string myFileName = "";

	cout << "Enter the file name you\'d like to read from: ";
	cin >> myFileName;

	myTeam.readFromFile(myFileName);

}


Function in class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
void Team :: readFromFile(string myFileName)
{

	string fileName = "";
	string myPlayerName = "";
	string myPlayerPos = "";
	string myPhoneNr = "";
	int myPlayerNr = 0;
	int myBirthYear = 0;
	int fileNrOfPlayers = 0;
	bool check = false;

	ifstream myFile;
	fileName = "C:/Users/KINA/Documents/" + myFileName + ".txt";

	myFile.open(fileName);

	if(myFile.is_open())
	{

		check = true;

	}
	else
	{

		cout << "File was not found!" << endl;

	}

	if(check == true)
	{

		myFile >> fileNrOfPlayers;

		nrOfPlayers += fileNrOfPlayers;

		if(nrOfPlayers > CAPACITY)
		{

			Player ** temp;

			CAPACITY += nrOfPlayers;

			temp = new Player*[CAPACITY];

			players = temp;

		}

		for(int i = 0; i < nrOfPlayers; i++)
		{

			getline(myFile, myPlayerName);
			myFile >> myPlayerPos;
			myFile >> myPlayerNr;
			myFile >> myBirthYear;
			getline(myFile, myPhoneNr);
			players[i] = new Player(myPlayerName, myPlayerPos, 
				myPhoneNr, myPlayerNr, myBirthYear);

		}

		myFile.close();

	}

}


After it's read, it looks like this

5

TT
0
0


TT
0
0


TT
0
0


TT
0
0


TT
0
0



Can someone help me figure out what the problem is? Been stuck on it for about 4 hours now and it's really grinding my gears.
When you read a number the '\n' in the buffer is not discarded, so `getline()' would interpret that as an empty string.
1
2
3
4
5
int number;
std::string line;
std::cin>>number;
std::cin.ignore(); //discard the '\n'
std::getline(std::cin, line);


Also
1
2
3
4
5
6
7
		if(nrOfPlayers > CAPACITY)
		{
			Player ** temp;
			CAPACITY += nrOfPlayers;
			temp = new Player*[CAPACITY];
			players = temp; //possible memory leak
		}
If I use cin.ignore() it just stands there waiting for me to press enter, getline(cin, line); I assume you mean getline(myFile, line)(?)

I'll fix the memory leak after I've gotten it to work (only a possible memory leak if players already have users in it) :)
You could force the program to crash when the input file stream goes into an error state. At least then you know if it's the file reading that is to blame, and not the memory leaks.

I removed your code to make the changes clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <ios>

using namespace std; // bad habit, unlearn it someday

// ...

void Team :: readFromFile(string myFileName) try
{
	// your code removed here

	ifstream myFile;
	fileName = "C:/Users/KINA/Documents/" + myFileName + ".txt";
	myFile.exceptions(ios_base::badbit | ios_base::failbit);
	myFile.open(fileName);

	// your code removed here
}
catch (const ios_base::failure &e)
{
    cerr << "File reading failure: " << e.what() << '\n';
    exit(EXIT_FAILURE);
}

If I put myFile >> on all of the getlines it work fine, so it's the getlines that are f*king around ^^
Last edited on
> If I use cin.ignore() it just stands there waiting for me to press enter,
> getline(cin, line); I assume you mean getline(myFile, line)(?)
That was an example, modify it according to the streams that you are using.
Ah yeah, fixed it all, even the leak as well by using the already existing addPlayer function (that creates a new pointer if needed).
Last edited on
Topic archived. No new replies allowed.