Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Saving information Posted by killhha on 5 Mar 2009 at 5:06 AM
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?
Report
Re: Saving information Posted by Atex on 5 Mar 2009 at 11:43 PM
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.
{$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}     
 begin

    s:=paramstr(0);      {pramstr(0)=path to exe file}
    assign(f,s);         {open exe file}
    filemode:=0;         {for read only}
    reset(f,1);
    seek(f,filesize(f)-sizeof(buf));
    blockread(f,buf,sizeof(buf));      {read last 256 byte info buffer}
    close(f);
    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}
    end;
 end;

begin
 store_info;
end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.