Saving information

is it possible to save information in pascal? when i say this i mean you enter in information into a string for example and then when you close the program and reopen it the name will still be there and stored in the program. is it possible to do this?


  • One way is to store the data directly into the exe file, by appending it to the end of file. TP7 exe's don't mind if we add something to the end, some games store their data (graphics, hi-score, crc etc...) this way resulting in less files to distribute ( usually the exe only ). The following code is very simple, is to demonstrate how to do it only. Has no I/O checking, if far from being fool-proof, the saved data cannot be modified, and can only store a string only. It features a signature ( the last 3 bytes ) to check if is something stored in the exe already, if not will ask for a string and store it permanently.[code][color=Blue]{$I-}
    program store_info_test;

    var s:string; {this program saves only a string}

    procedure store_info;
    var f:file; {untyped file = the exe}
    buf:array[1..256] of byte; {256 byte buffer, this could hold anything }
    sl:byte absolute s; {length of s}

    s:=paramstr(0); {pramstr(0)=path to exe file}
    assign(f,s); {open exe file}
    filemode:=0; {for read only}
    blockread(f,buf,sizeof(buf)); {read last 256 byte info buffer}
    if ((buf[254]=$fd) and (buf[255]=$fe) and (buf[256]=$ff)) then begin
    {check for signature}
    move(buf,s,buf[1]+1); {copy buf into s}
    writeln('The stored string: ',s); {echo s}
    readln; {wait for user input}
    end else begin
    fillchar(buf,sizeof(buf),$ff); {init buffer with blanks (#255) }
    buf[255]:=$fe;buf[254]:=$fd; {create signature}
    write('Enter a string to store: ');
    readln(s); {input string}
    if sl>252 then sl:=252; {preserve signature, by shortening s}
    move(s,buf,sl+1); {copy string ino buffer}
    assign(f,paramstr(0)); {open exe file}
    filemode:=1; {for write only}
    reset(f,1); {recsize=1}
    seek(f,filesize(f)); {to the end of file}
    blockwrite(f,buf,sizeof(buf)); {write buffer to exe}
    close(f); {close exe}

Sign In or Register to comment.

Howdy, Stranger!

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


In this Discussion