Grrr...I can't get this file to load!
I was wondering if anyone could lend me a hand? I''m having trouble getting a text file to load into my program.
I commented out the strings (that''s another problem I need to contend with in the future).
#include "Globals.h"
#include "Player.h"
//--------------------------------------------------------------
// int LoadPlayer(Player players[])
//--------------------------------------------------------------
int LoadPlayer(Player players[])
{
Player p;
int i;
FILE* f = fopen("playerdata.txt", "r");
if (f ==NULL)
{
fprintf(stderr, "Could not open playerdat.txt\n");
return 0;
}
for (i = 0; i < NUM_TOTAL_PLAYERS && fread &p, sizeof(p), 1, f) == 1; ++1)
{
players.ID = p.ID;
// strcpy(players.LastName, p.LastName);
// strcpy(players.FirstName, p.FirstName);
// strcpy(players.MiddleInitial, p.MiddleInitial);
// strcpy(players.CurrentTeam, p.CurrentTeam);
players.BWalks = p.BWalks;
players.BIntWalks = p.BIntWalks;
players.BAtBats = p.BAtBats;
players.BHitByPitch = p.BHitByPitch;
players.BBattingAvg = p.BBattingAvg;
players.BStrikeOuts = p.BStrikeOuts;
players.BStolenBases = p.BStolenBases;
players.BCaughtStealing = p.BCaughtStealing;
players.BDoubles = p.BDoubles;
players.BTriples = p.BTriples;
players.BHomeRuns = p.BHomeRuns;
players.BHits = p.BHits;
players.BGroundFlyRatio = p.BGroundFlyRatio;
players.BDoublePlays = p.BDoublePlays;
players.FCERA = p.FCERA;
ETCβ¦β¦
}
fclose(f);
return i;
}
//βββββββββββββββββββββ
// void SavePlayer(Player players[], int nplayers)
//βββββββββββββββββββββ
void SavePlayer(Player players[], int nplayers)
{
int i;
FILE* f = fopen("outfile.txt", "w");
if (f == NULL || fwrite(players, sizeof(Player), nplayers, f) != nplayers)
{
fprintf(stderr, "Could not save outfile.txt\n");
return;
}
fclose(f);
}
ββββββββββ
The part where i''m calling them is thisβ¦
Player players[NUM_TOTAL_PLAYERS];
int nplayers = LoadPlayer(players);
// GameState initialization
if (G1.PrevGameState != GS_VERSUS)
{
G1.PrevGameState = GS_VERSUS;
}
// Display what the pitcher / batter matchup is
hdc = GetDC(G1.hWnd);
SetBkColor(hdc, RGB(0, 0, 0));
SetTextColor(hdc, RGB(255, 255, 255));
sprintf(buffer,"This is a test %d %d.",
players[1].ID, players[2].BWalks);
TextOut(hdc, 5, 5, buffer, strlen(buffer));
ReleaseDC(G1.hWnd,hdc);
I''m getting jibberish in the place of what the actual numbers should be.
The txt file data is listed like thisβ¦
1
61
20
529
2
0
50
5
2
51
3
21
197
0
8
0
0
0
ETCβ¦
Any help would be most appreciated. </i>
i know whats wrong .. u are reading the entire player struct from a text file .. with text.. sorry , fread dont work like that....
try this ..
// save this first
Player player;
player.name = "myname";
fwrite(&player,sizeof(Player),1,file);
// then only u can read with fread
Player player
fread(&player.sizoef(Player),1,file);
unless you use fscanf, u cant convert strings in the file into integer values..
{ Stating the obvious never helped any situation !! }
try this ..
// save this first
Player player;
player.name = "myname";
fwrite(&player,sizeof(Player),1,file);
// then only u can read with fread
Player player
fread(&player.sizoef(Player),1,file);
unless you use fscanf, u cant convert strings in the file into integer values..
{ Stating the obvious never helped any situation !! }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement