|
Problems reading and writeing to a datafile
i am making a game for a school project and am having problems with my highscores system. When you die (thus activating highscores code) i get an error message that says file access denied. I orginally thought it was due to the highscores page trying to read it at the same time, so i removed this form, however it still came up with the same error message. Any help would be greatly apreciated.
i declared the record and variable types as follows:
Type
HighScores = record
Name: String[15];
Score: Integer;
end;
Var HighScore: array[1..10] of HighScores;
ScoresFile: file of HighScores;
HighScoresdata: HighScores;
and the highscores code is:
AssignFile(ScoresFile, 'Scores.dat');
reset(ScoresFile);
while not eof(ScoresFile) do
begin
read(ScoresFile, HighScoresData);
end;
if Score > HighScore[10].Score then
begin
HighScore[10].Score:=Score;
HighScore[10].Name:=InputBox('HighScores', 'Congradulations you got a high score!', 'Enter your name here');
HighScoresCont := true;
end;
while HighScoresCont do
begin
HighScoresCont := false;
for HSSort:= 1 to 10 do
if HighScore[HSSort].Score < HighScore[HSSort+1].Score
then begin
HSTemp := HighScore[HSSort].Score;
HighScore[HSSort].Score := HighScore[HSSort+1].Score;
HighScore[HSSort+1].Score := HSTemp;
HighScoresCont := true;
end; {if}
end; {while}
Reset(ScoresFile);
write(ScoresFile, HighScoresData); {this line is highlighed in blue when i get the error message}
CloseFile(ScoresFile);
if InputName = True then
begin
ViewHS := MessageDLg('Do you want to view highscores?', mtConfirmation, [mbYes, mbNo], 0) = mrYes;
if ViewHS then
begin
FrmHighScores.show;
FrmMainGame.Visible:=False;
end;
end;
end;
end;
|