Help needed, Please!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help needed, Please!!!

der_rangader_ranga Posts: 42Member
Help!!!
Really struggling with this. Below i have a chunk of code from a program, assuming all external references are correct, and the file 'teamfile' exists and is pointed to correctly. Can anyone see why on the second time of executing the procedure 'readteamdets' that a file I/O error 103 would occur?




procedure readteamdets(I,J:integer;var team1 : teamdets;var team2 : teamdets;tfilestring : string);
var teamfile : file of teamdets;
begin
assignfile(teamfile,tfilestring);

reset(teamfile);
seek(teamfile,(i-1)); // Use Team (I's) Details
read(teamfile,team1);

reset(teamfile);
seek(teamfile,(j-1)); // Use Team (J's) Details
read(teamfile,team2);
closefile(teamfile);
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
var i : integer; //row of matchday array
j : integer; //column of matchday array
m : integer; // loop variable for gamescores;
b : integer; // loop variable for bowlers lineup;
k : integer; // loop variable for bowlers lineup;
x : integer;
y : integer; // loop variable for number matchdays required;
matches : integer;
tmp : string;
bowler : bowlerdets;
bowlfile : file of bowlerdets;
teamfile : file of teamdets;
match : matchinfo;
matchfile : Textfile;
matchstring : string;
matchDay : array[1..4,1..4] of integer;
team1 : teamdets;
team2 : teamdets;
tfilestring : string;
begin
tmp := '';
for i := 1 to 3 do tmp := tmp + league.leagueName[i];
matches := 0;
randomize;

//loop creates initial setup of matchday array
for i := 1 to league.TeamsInput do
for j:= 1 to league.TeamsInput do
begin
if j > i then matchday[i,j] := 0
else matchday[i,j] := -1;
end;

tfilestring := 'c:TPManage'+inttostr(league.startyear)+''+tmp+'team.Tpd';
assignfile(bowlfile,'c:TPManage'+inttostr(league.startyear)+''+tmp+'play.Tpd');
// assignfile(teamfile,'c:TPManage'+inttostr(league.startyear)+''+tmp+'team.Tpd');
// this loop is ussed for the matchday number & date
for y:=1 to (league.TeamsInput-1) do
begin
repeat
{repeat until number of allocated matches
= number matches needed per match day}

repeat
{picks random I & J values with conditions :
-----I must be greater than J
-----matchday[i,j] must be unallocated '0'}
i := random(league.TeamsInput) +1;
j := random(league.TeamsInput)+1;
until (j>i) and (matchday[i,j] = 0);

matchday[i,j] :=2; //set matchday[i,j] to allocated '2'

//Now to prevent team I/J playing again today :
for x:=1 to league.TeamsInput do
begin
if matchday[i,x] = 0 then matchday[i,x]:=1;
if matchday[x,i] = 0 then matchday[x,i]:=1;
if matchday[j,x] = 0 then matchday[j,x]:=1;
if matchday[x,j] = 0 then matchday[x,j]:=1;
end;

matches := matches + 1; // number of matches allocated currently
until (matches = (league.TeamsInput div 2));
showmessage('num matches allocated : '+inttostr(matches));

{now to extract necessary information for a single match day using the array that maps the matches}
match.matchID := y;
// reset(teamfile);
for i:=1 to league.TeamsInput do
for j:=1 to league.TeamsInput do
if (j>i) and (matchday[i,j] = 2) then
begin
matchday[i,j] := 3; //set match I&J to played already '3'
showmessage('I := ' + inttostr(i) + 'J := '+ inttostr(j));
{********Retrieves team 1 And 2's details from teamfile********}
readteamdets(I,J,team1,team2,tfilestring);

{**********Store team 1's (i's) Details into Match variable **********}
// showmessage('team : '+team1.name);
match.ID1 := team1.teamID;
match.team1name := team1.name;
for k:=1 to league.bowlPerMatch do
begin
// showmessage('K : '+inttostr(k)+'lineup[k] : '+inttostr(team.lineup[k]));
reset(bowlfile);
if team1.lineup[k] <> -1 then seek(bowlfile,team1.lineup[k]-1);
read(bowlfile,bowler);
match.team1[k].ID := bowler.bowlerID;
match.team1[k].firstname := bowler.firstName;
match.team1[k].surname := bowler.surname;
end;
closefile(bowlfile);

{**********Store team 2's Details into Match variable **********}
match.ID2 := team2.teamID;
match.team2name := team2.name;
// showmessage('team : '+team.name);

for k:=1 to league.bowlPerMatch do
begin
// showmessage('K : '+inttostr(k)+'lineup[k] : '+inttostr(team.lineup[k]));
reset(bowlfile);
if team2.lineup[k] <> -1 then seek(bowlfile,team2.lineup[k]-1);
read(bowlfile,bowler);
match.team2[k].ID := bowler.bowlerID;
match.team2[k].firstname := bowler.firstName;
match.team2[k].surname := bowler.surname;
end;
closefile(bowlfile);
showmessage('Match between... team1 : '+match.team1name+' team2 : '+match.team2name);

{********creating the matchstring to write to the textfile ********}
matchstring := inttostr(match.ID1)+','+match.team1name+',';
for b := 1 to 10 do
begin
if match.team1[b].firstname <> '' then
begin
matchstring := matchstring + inttostr(match.matchID) + inttostr(match.team1[b].ID) +','+ match.team1[b].Firstname +','+ match.team1[b].surname+',';
for m := 1 to 6 do matchstring := matchstring + inttostr(match.team1[b].gamescores[m]) +',';
end;
end;
matchstring := matchstring+inttostr(match.ID2)+','+match.team2name+',';
for b := 1 to 10 do
begin
if match.team1[b].firstname <> '' then
begin
matchstring := matchstring + inttostr(match.team2[b].ID) +','+ match.team2[b].Firstname +','+ match.team2[b].surname+',';
for m := 1 to 6 do matchstring := matchstring + inttostr(match.team2[b].gamescores[m]) +';';
end;
end;
showmessage(matchstring);
//assigns the match file handler to correct file
assignfile(matchfile,'c:TPManage'+tmp+inttostr(match.matchID)+'.Tpd');
{$IOChecks off}
reset(matchfile);
try
append(matchfile);
writeln(matchstring);
except
rewrite(matchfile);
writeln(matchfile,matchstring);
end;
closefile(matchfile);
{$IOChecks ON}
DisplaySched;
end;
end;

end;

cheers Der_ranga

Comments

  • jobromediajobromedia Posts: 168Member
    : Help!!!
    : Really struggling with this. Below i have a chunk of code from a program, assuming all external references are correct, and the file 'teamfile' exists and is pointed to correctly. Can anyone see why on the second time of executing the procedure 'readteamdets' that a file I/O error 103 would occur?
    :
    I'm in no way an expert, but did you close the teamfile after reading it the first time?

    Could be so simple as that.
Sign In or Register to comment.