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.