![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
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; |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I'd love to help, but that's currently impossible to read. Please use [code] tags - they preserve the formatting of the code.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
sorry i hope this is better...
record decleartion and file variables: implementation
uses Unit1, Unit3;
Type
HighScores = record
Name: String[15];
Score: Integer;
end;
Var HighScore: array[1..10] of HighScores;
ScoresFile: file of HighScores;
HighScoresdata: HighScores;other highscores code:
AssignFile(ScoresFile, 'Scores.dat'); {assigns the variable 'Scores File' to the file 'Scores.dat' on the HDD}
reset(ScoresFile); {resets the file pointer to the begining of the file}
while not eof(ScoresFile) do
begin
read(ScoresFile, HighScoresData); {reads all of the data on the file to the highscores record}
end; {while}
if Score > HighScore[10].Score then {if player's score is greater than the 10th highscore than the player's score becomes the 10th highscore}
begin
HighScore[10].Score:=Score;
HighScore[10].Name:=InputBox('HighScores', 'Congradulations you got a high score!', 'Enter your name here'); {player's name is entered into the record}
HighScoresCont := true; {condition to start the sorting process is set to true}
Inputname:=True; {condition to allow option to view highscores}
end; {if}
while HighScoresCont do {while the highscorescont is true the program will sort the highscores from highest to lowest}
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); {resets the file pointer to the begining of the file}
write(ScoresFile, HighScoresData); {the highscores record is written to disk} {this line is highlighted in blue when the error message occurs}
CloseFile(ScoresFile); {closes 'ScoresFile', saving any new information}
if InputName = True then {if name has been entered then option to view highscores is given to the player}
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; |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
Changed code
ok i changed the code around a bit its now:
{HighScores}
AssignFile(ScoresFile, 'Scores.dat'); {assigns the variable 'Scores File' to the file 'Scores.dat' on the HDD}
reset(ScoresFile); {resets the file pointer to the begining of the file}
while not eof(ScoresFile) do
begin
read(ScoresFile, HighScoresData); {reads all of the data on the file to the highscores record}
end; {while}
CloseFile(ScoresFile);
if Score > HighScore[10].Score then {if player's score is greater than the 10th highscore than the player's score becomes the 10th highscore}
begin
begin
HighScore[10].Score:=Score;
HighScore[10].Name:=InputBox('HighScores', 'Congradulations you got a high score!', 'Enter your name here'); {player's name is entered into the record}
HighScoresCont := true; {condition to start the sorting process is set to true}
end;
while HighScoresCont do {while the highscorescont is true the program will sort the highscores from highest to lowest}
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}
Inputname:=True; {condition to allow option to view highscores}
AssignFile(ScoresFile, 'Scores.dat');
Reset(ScoresFile); {resets the file pointer to the begining of the file}
write(ScoresFile, HighScoresData); {the highscores record is written to disk} {this line is highlighted in blue when the error message occurs}
CloseFile(ScoresFile); {closes 'ScoresFile', saving any new information}
end; {if get highscore greater than highscore[10]}
if InputName = True then {if name has been entered then option to view highscores is given to the player}
begin
InputName:=False;
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;and now i dont get the error message.. HOWEVER i have new problems.. 1. i have to enter my name 10 times, i would like ot only enter it once... 2. the scores do not get displayed on the highscores page. the code for the highscores page is procedure TFrmHighScores.FormCreate(Sender: TObject);
begin
if FrmHighScores.Visible=True then
begin
AssignFile(ScoresFile, 'Scores.dat');
Reset(ScoresFile);
while not eof(ScoresFile)do
begin
read (ScoresFile, HighScoresData);
end;
LblName1.Caption:=HighScore[1].Name;
LblName2.Caption:=HighScore[2].Name;
LblName3.Caption:=HighScore[3].Name;
LblName4.Caption:=HighScore[4].Name;
LblName5.Caption:=HighScore[5].Name;
LblName6.Caption:=HighScore[6].Name;
LblName7.Caption:=HighScore[7].Name;
LblName8.Caption:=HighScore[8].Name;
LblName9.Caption:=HighScore[9].Name;
LblName10.Caption:=HighScore[10].Name;
LblScore1.Caption:=IntToStr(HighScore[1].Score);
LblScore2.Caption:=IntToStr(HighScore[2].Score);
LblScore3.Caption:=IntToStr(HighScore[3].Score);
LblScore4.Caption:=IntToStr(HighScore[4].Score);
LblScore5.Caption:=IntToStr(HighScore[5].Score);
LblScore6.Caption:=IntToStr(HighScore[6].Score);
LblScore7.Caption:=IntToStr(HighScore[7].Score);
LblScore8.Caption:=IntToStr(HighScore[8].Score);
LblScore9.Caption:=IntToStr(HighScore[9].Score);
LblScore10.Caption:=IntToStr(HighScore[10].Score);
CloseFile(ScoresFile);
end
end;the record and file variables are declared exactly the same as htey are for the first section of code (main game code) if anybody can help it would be greatly apriecated...i dont have much time left to hand this in... |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You're reading the high scores into HighScoresData, and then overwriting them with the next score. There's nothing in the HighScore array.
i = 1;
while not eof(ScoresFile)do
begin
read (ScoresFile, HighScoresData);
HighScore[i] := HighScoresData;
i := i + 1;
end; |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
i replace the code but i still get the same problem except this time its not 10 times the name entering box comes up it just keeps coming up indefinately, i have to ctrl+alt+del to get out of the program, as i am a beginer i realy dont understand what is happening, ive been trying to solve the problem but have gotten no where.
i thank you for the help you have given me thus far and hope you have the time to continue helping. |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Can I have the whole code for the first function? I think there's a bit missing at the beginning.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
its in a timer,
this is the code for all the timer, got some background movement bits in it as well: procedure TFrmMainGame.Timer1Timer(Sender: TObject);
begin
begin {Right Background Loop}
if backgroundLeft.Top >0
then BackgroundLeft.Top:=-2015
else BackgroundLeft.Top:=BackgroundLeft.Top + 5;
end; {Right Background Loop}
Begin {Left Background Loop}
if backgroundRight.Top >0
then backgroundRight.Top:=-2015
else BackgroundRight.Top:=BackgroundRight.Top + 5;
end; {Left Background Loop}
RedDot1.Top:=RedDot1.Top + 5;
RedDot2.Top:=RedDot2.Top + 5;
Missile1.Top:=Missile1.top + 12;
Missile2.Top:=Missile2.top + 12;
{stops plane from going off left hand side of screen}
if planeimage.left<144 then planeimage.left:=144;
{stops plane from going off right hand side of screen}
if planeimage.left>520 then planeimage.Left:=520;
{stops plane from going off the top of thescreen}
if planeimage.top<1 then planeimage.Top:=1;
{stops plane from going off bottom of the screen}
if planeimage.top>562 then planeimage.top:= 562;
{GameOver Sequence}
If Gameover = True then
begin
TimerMovement.Enabled:=False;
Missile1Timer.Enabled:=False;
Missile2Timer.Enabled:=False;
RedDot1Timer.Enabled:=False;
RedDot2Timer.Enabled:=False;
LblGameOver.Visible:=True;
{HighScores}
AssignFile(ScoresFile, 'Scores.dat'); {assigns the variable 'Scores File' to the file 'Scores.dat' on the HDD}
reset(ScoresFile); {resets the file pointer to the begining of the file}
i := 1;
while not eof(ScoresFile)do
begin
read (ScoresFile, HighScoresData);
HighScore[i] := HighScoresData;
i := i + 1;
end;
if Score > HighScore[10].Score then {if player's score is greater than the 10th highscore than the player's score becomes the 10th highscore}
begin
HighScore[10].Score:=Score;
HighScore[10].Name:=InputBox('HighScores', 'Congradulations you got a high score!', 'Enter your name here'); {player's name is entered into the record}
HighScoresCont := true; {condition to start the sorting process is set to true}
while HighScoresCont do {while the highscorescont is true the program will sort the highscores from highest to lowest}
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}
Inputname:=True; {condition to allow option to view highscores}
Rewrite(ScoresFile); {resets the file pointer to the begining of the file}
write(ScoresFile, HighScoresData); {the highscores record is written to disk} {this line is highlighted in blue when the error message occurs}
end; {if get highscore greater than highscore[10]}
closefile(ScoresFile);
if InputName = True then {if name has been entered then option to view highscores is given to the player}
begin
InputName:=False;
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; |
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Welp, I didn't like the way the code was formatted, so I "fixed" it. Feel free to take it or leave it, but note that it did help me find the error.
procedure TFrmMainGame.Timer1Timer(Sender: TObject);
begin
begin {Right Background Loop}
if backgroundLeft.Top > 0 then
BackgroundLeft.Top := -2015
else
BackgroundLeft.Top := BackgroundLeft.Top + 5;
end; {Right Background Loop}
begin {Left Background Loop}
if backgroundRight.Top > 0 then
backgroundRight.Top := -2015
else
BackgroundRight.Top := BackgroundRight.Top + 5;
end; {Left Background Loop}
RedDot1.Top := RedDot1.Top + 5;
RedDot2.Top := RedDot2.Top + 5;
Missile1.Top := Missile1.top + 12;
Missile2.Top := Missile2.top + 12;
{stops plane from going off left hand side of screen}
if planeimage.left<144 then planeimage.left:=144;
{stops plane from going off right hand side of screen}
if planeimage.left>520 then planeimage.Left:=520;
{stops plane from going off the top of thescreen}
if planeimage.top<1 then planeimage.Top:=1;
{stops plane from going off bottom of the screen}
if planeimage.top>562 then planeimage.top:= 562;
{GameOver Sequence}
If Gameover = True then
begin
TimerMovement.Enabled:=False;
Missile1Timer.Enabled:=False;
Missile2Timer.Enabled:=False;
RedDot1Timer.Enabled:=False;
RedDot2Timer.Enabled:=False;
LblGameOver.Visible:=True;
{HighScores}
{assigns the variable 'Scores File' to the file 'Scores.dat' on the HDD}
AssignFile(ScoresFile, 'Scores.dat');
{resets the file pointer to the begining of the file}
reset(ScoresFile);
i := 1;
while not eof(ScoresFile)do
begin
read (ScoresFile, HighScoresData);
HighScore[i] := HighScoresData;
i := i + 1;
end;
{if player's score is greater than the 10th highscore than the player's score becomes the 10th highscore}
if Inputname = False then
begin
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'); {player's name is entered into the record}
HighScoresCont := true; {condition to start the sorting process is set to true}
while HighScoresCont do {while the highscorescont is true the program will sort the highscores from highest to lowest}
begin
HighScoresCont := false;
for HSSort:= 1 to 10 do
begin
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;
end; {while}
Inputname := True; {condition to allow option to view highscores}
Rewrite(ScoresFile); {resets the file pointer to the begining of the file}
write(ScoresFile, HighScoresData); {the highscores record is written to disk} {this line is highlighted in blue when the error message occurs}
end; {if get highscore greater than highscore[10]}
end;
end;
closefile(ScoresFile);
if InputName = True then {if name has been entered then option to view highscores is given to the player}
begin
InputName := False;
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;The problem was that you weren't checking to see if the name had already been inputted before you asked. Take a look at the code highlighted in green. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: May 2006
Posts: 9
Rep Power: 0
![]() |
thank you for formatting it, i havn't had much time to all of this so im still how you say a noob.
ummm, it makes sense why it should work now, but it still doesnt' for some reason.... which i really dont understand, if it would be of help i could send the program to you if you provide me with an email address. but i think all the code that should effect the problem lies with what i have already posted. i once again thank you for your help, i appreciate what you have done for me so far. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|