Why is this Breaking?

My Program keeps breaking whenever I run the file. It will only read the fist line of the file then break. PLEASE help.

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
  #include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int NAM_LEN = 40;
const int TEAM_LEN = 10;
const int POS_LEN = 10;

struct player_info
{
	char playerName[NAM_LEN];
	char team[TEAM_LEN];
	char position[POS_LEN];
	char ownerName[NAM_LEN];
};



int main()	
{	
	const int MAX_PLAYERS = 20;
	player_info player[MAX_PLAYERS];

	ifstream inFile;
	inFile.open("fantasy.dat");

	cout<<"Player Name\t\tTeam\t\tPosition\t\tOwner Name"<<endl;
	cout<<"-----------\t\t-----\t\t-------\t\t\t----------"<<endl;


	for(int i=0; i < sizeof(player); i++)
	{	
		inFile.getline(player[i].playerName, NAM_LEN);
		inFile.ignore();

		inFile.getline(player[i].team, TEAM_LEN);
		inFile.ignore();

		inFile.getline(player[i].position, POS_LEN);
		inFile.ignore();

		inFile.getline(player[i].ownerName, NAM_LEN);
		inFile.ignore();
		
	/*}

	
	for(int j=0; j < sizeof(player); j++)
	{	*/
	
		cout<<player[i].playerName<<endl;//<<"\t\t"<<player[i].team<<"\t\t"<<player[i].position<<"\t\t"<<player[i].ownerName<<endl;
	
	}


	inFile.close();
	
	system("pause");

	return 0;

}


The file should read and out put this in the categories above.

File: fantasy.dat
ANDERSON SF K JOANN
TIMPSON PHIL WR JIM
ELWAY DEN QB JACKV
SALAAM CHI RB BILL
CARNEY SD K VINCE
SANDERS DET RB JOHN
CARTER MINN WR JACKR
FREEMAN GB WR RON
WATTERS PHIL RB CHRIS
CHMURA GB TE STEVE
BLEDSOE NE QB CHRIS
FAULK IND RB JIM
GEORGE TENN RB STEVE
BETTIS PITT RB JACKV
STEWART PITT QB BILL
DUDLEY OAK TE JOHN
MARINO MIA QB VINCE
KIRBY SF RB RON
WARREN SEA RB JACK
MARTIN NE RB JOANN


You have a few issues here.

sizeof(player) isn't going to be 20, you're better off using MAX_PLAYERS instead.

inFile.getline(player[i].playerName, NAM_LEN); isn't going to be what you want, either. That will take everything until it reaches the end of that line in the file, or the number of characters equal to NAM_LEN, whichever happens first. Two solutions is to either add a delimiter, or to use inFile.get instead. I suggest specifying your delimiter, whichever route you go, so that you can properly handle the end of line character as well.

1
2
	ifstream inFile;
	inFile.open("fantasy.dat");

This actually caused me a few WTFs earlier (it's 4am, cut me some slack). I didn't have the file in the right directory, and since the program doesn't check to make sure the file opened, it read a bunch of random bits in memory and gave me some very weird results. I highly suggest you add a check to make sure the file is open

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int i=0; i < sizeof(player); i++)
	{	
		inFile.getline(player[i].playerName, NAM_LEN);
		inFile.ignore();

		inFile.getline(player[i].team, TEAM_LEN);
		inFile.ignore();

		inFile.getline(player[i].position, POS_LEN);
		inFile.ignore();

		inFile.getline(player[i].ownerName, NAM_LEN);
		inFile.ignore();
		
	/*} */

While this works in most cases, you need to make sure you're not reading passed the EOF. This can also cause headaches on improperly formatted files.

I believe the main issue you've been having is the getline part, and going out of bounds on your array as well, I got SEGFAULT a lot due to going out of bounds due to what getline was reading, and not allowing the data to be stored into the other variables. Also, the sizeof() was reading MUCH larger than you wanted, allowing more than 20 elements of players to be accessed.

If you fix those problems, or need help doing so, and still run into issues, please let me know.
Topic archived. No new replies allowed.