Skip to main content
gamedev.net
🔒 Locked

file saving?

Started by buttons Jan 7, 2002 at 5:09 PM 2 replies 350+ views
Original Post
buttons
buttons
I love Delphi and I''m learning a lot about it, but I''m still pretty new. I was hoping someone could help me out with some help on saving files. I kind of understand it but wanted some examples. what i really want to do is to be able to parse a text file that I create and display the information from that file in the proper places (ex. Player name, score, level, etc.). How does one save that file, and then how do they parse it back? Buttons
Zimond
Zimond
I wrote some functions specially for handling config ascii files...

Writing to a file :

USAGE EXAMPLE : ascii_write (''filename.sav'',''Player Name : '',''Berserker'');

function ascii_write (datei,suchstring,schreibstring:string):boolean;
var f,ueber:textfile;
lies:string;
gefunden : boolean;
begin
gefunden := false;
if fileexists(datei) then
begin
assignfile(f,datei);
assignfile(ueber,''copy.tem'');
reset(f);
rewrite(ueber);

while not eof(f) do
begin
readln(f,lies);
if copy(lies,1,length(suchstring)) = suchstring
then begin
gefunden := true;
writeln(ueber,suchstring+schreibstring)
end
else writeln(ueber,lies);
end;
if not gefunden then writeln(ueber,suchstring+schreibstring);

closefile(f);
closefile(ueber);
erase(f);
rename(ueber,datei)
end
else
begin
assignfile(f,datei);
rewrite(f);
writeln(f,suchstring+schreibstring);
closefile(f);
end;
result := true;
end;


Checking items :
This will check if an item exists or if your app has to use a default value.
USAGE EXAMPLE : check_ascii (''file.sav'',''Playername : '');

function check_ascii (datei,entry:string) : boolean;
var f :textfile;
lies1:string;
hilfsbool:boolean;
begin // function to check if an entry exists in an textfile.
hilfsbool := false;
assignfile(f,datei);
reset(f);
while not eof(f) do begin
readln(f,lies1);
if copy(lies1,1,length(entry)) = entry then
hilfsbool := true;
end;
closefile(f);
// bool to result
result := hilfsbool;
end;


... and reading.
The result is the part behind "Playername : "

USAGE EXAMPLE : ascii_read (''file.sav'',''Playername : '');


function ascii_read (datei,entry:string) :AnsiString;
var f:textfile;
lies1:string;
begin // function that reads an entry of an ascii-file
assignfile(f,datei);
reset(f);
while not eof(f) do begin
readln(f,lies1);
if copy(lies1,1,length(entry)) = entry then
result := copy(lies1,length(entry)+1,999);
end;
closefile(f);
end;
MButchers
MButchers
Hello there,

look up

TIniFile
TStringList

these have all the functionallity built in for loading
configuration files.

Hope it helps

Mark

buttons
buttons
Those are both great. Thanks.
Buttons

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.