SRL Archive - Help and Support

Hehe, Sorry, More File Handling Help...


Timer

2008-01-31 23:00:59

Ok, More File Handling :p, How Would I Open A File, Then Have The File Become A String, Then (This Is Wat) Use Replace To Replace A Part Of The String, Then Re-Save The File ( Still Containing What Was Also In There, Only Change, In This Problem, The Score.

  TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\Bejewled Highscore.txt', False);
ReadFileString(TheFile, s, FileSize(TheFile));
Closefile(TheFile);
if (Score * 10) > StrToInt(Between('[Bejeweled HighScore]', '[/Bejeweled HighScore]', S)) then
begin
TheFile := ReWriteFile('C:\Documents and Settings\Joe\Desktop\HighScores.txt', False);
WriteFileString(TheFile, '[Bejeweled HighScore]' + IntToStr(Score * 10) + '[/Bejeweled HighScore]]');
Writeln('New HIGHSCORE!');
CloseFile(TheFile);
end;

so like..
  TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\Bejewled Highscore.txt', False);
ReadFileString(TheFile, s, FileSize(TheFile));
Closefile(TheFile);
if (Score * 10) > StrToInt(Between('[Bejeweled HighScore]', '[/Bejeweled HighScore]', S)) then
begin
TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\HighScores.txt', False);
WriteFileString(TheFile, Replace('[Bejeweled HighScore]', IntToStr(Score * 10), '[/Bejeweled HighScore]]'));
Writeln('New HIGHSCORE!');
CloseFile(TheFile);
end;

Help!? :(
Thanks
- Timer -

BobboHobbo

2008-02-01 01:03:18

HEHEH RIPS HAIR OUT!!!!!!!!!!!!!!!!!! STOP USING THAT WORD :)

[/offtopic]

Negaal

2008-02-01 06:04:43

Dude, please explain more, or post whole procedure please…can't understand what are you looking for atm…and why did you posted same thingy 2 times?

Anyways, this thing should write score…I don't know did I understood right what you want
program New;

var
Score, BestScore : String;

begin
Score := IntTostr(129{The score you'll get})
if not FileExists(ScriptPath+'\Score.ini') then
WriteINI('Score', 'ScoreNr', Score, ScriptPath+'\Score.ini');

BestScore := ReadINI('Score', 'ScoreNr',ScriptPath+'\Score.ini');
if StrToInt(BestScore) < StrToInt(Score) then
begin
WriteINI('Score', 'ScoreNr', Score, ScriptPath+'\Score.ini');
BestScore := ReadINI('Score', 'ScoreNr',ScriptPath+'\Score.ini')
end;

Writeln('BestScore = '+BestScore);
end.

Edit: Im agree with BH

Sumilion

2008-02-01 08:43:02

If you just want to read and write things then it's best if you use WriteINI and ReadINI.

The variable for the path in these functions are entire paths, so not 'Settings.INI' for example … it should something like :

ScriptPath + 'Settings.INI'

or

'C:\Settings.INI'

If you really want to use the way you described it, tell me, then I can whip up an example.

Timer

2008-02-01 09:20:50

Wait tho, I dont want a INI :p

here.. umm

somthing like, this but it would compile… and do the right thing…

  TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\Highscores.txt', False);
ReadFileString(TheFile, s, FileSize(TheFile));
Closefile(TheFile);
if (Score * 10) > StrToInt(Between('[Bejeweled HighScore]', '[/Bejeweled HighScore]', S)) then
begin
TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\HighScores.txt', False);
S := Replace(Score*10{the score}, '[Bejeweled HighScore]'{bettween this}, '[/Bejeweled HighScore]'{and this}, S{of the string S});
WriteFileString(TheFile, S);
Writeln('New HIGHSCORE!');
CloseFile(TheFile);
end;

Sumilion

2008-02-01 11:29:37

Ok I don't have a lot of time on my hands at the moment but here is what you should do in words :

Use pos(); to figure out where exactly in the text the score starts and ends. Startposition = pos('[Bejeweled HighScore]') + the length of the string itself, as that is where the highscore starts. Endposition = pos('[/Bejeweled HighScore]'); perhaps with a - 1 I'm not entirely sure.

The difference between StartPosition and Endposition is the length of your highscore. Then you can use delete(); to delete that part of the string and then insert(); to insert the new highscore.

Hope that tells you how to handle it, if not let me know again and I'll take more time for it then to explain it better.

Timer

2008-02-01 11:40:12

Howdo i handle Delete and Insert?
Somthing Like?

var
thefile:integer;
s, Scoreteh:string;

begin
TheFile := OpenFile('C:\Documents and Settings\Joe\Desktop\Highscores.txt', False);
ReadFileString(TheFile, s, FileSize(TheFile));
Closefile(TheFile);
if (Score * 10) > StrToInt(Between('[Bejeweled HighScore]', '[/Bejeweled HighScore]', S)) then
begin
Scoreteh := Between('[Bejeweled HighScore]', '[/Bejeweled HighScore]', S);
S := Delete(ScoreTeh{delete this}, '[Bejeweled HighScore]'{from bettween this}, '[/Bejeweled HighScore]'{and this});
S := Insert(Score * 10,{insert this} '[Bejeweled HighScore]'{bettween this and}, '[/Bejeweled HighScore]'{this});
TheFile := ReWriteFile('C:\Documents and Settings\Joe\Desktop\HighScores.txt', False);
WriteFileString(TheFile, S);
Writeln('New HIGHSCORE!');
CloseFile(TheFile);
end;
end.

Sumilion

2008-02-01 13:04:46

Take a look at this example :

program New;

var
s: string;
posStart, posEnd: Integer;

begin
s := '[Bejeweled HighScore]500[/Bejeweled HighScore]';
posStart := pos('[Bejeweled HighScore]', s) + Length('[Bejeweled HighScore]');
posEnd := pos('[/Bejeweled HighScore]', s);
Delete(s, posStart, posEnd - posStart);
Insert('1000', s, posStart);
Writeln(s);
end.

It's a simplified script of what (afaik) you need.

Timer

2008-02-01 13:33:20

Line 500: [Error] (503:84): Type mismatch in script C:\Documents and Settings\Joe\Desktop\Grandmas Bejeweled.scar

For

Insert((Score * 10), s, posStart);

Timer

2008-02-01 13:38:54

wait, nvm…

TYSVM Sumilion!

Close