Results 1 to 3 of 3

Thread: Saving many records to file

  1. #1

    Saving many records to file

    Im not newbie in programming, just trying to learn more every day with my experiments.
    I need to create one specific file, like described in documenttation of file format. But my problem is how to write many records in file? Look my code.
    Code:
    type
       tmy_file_header=record
          file_type:array[0..3] of Char;
          vers_code:Word;
    end;
    
    mfh: tmy_file_header;
    f: file of tmy_file_header;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       assignfile(f,'c:\data.dat');
       {$I-}
          reset(f);
          seek(f, filesize(f));
       {$I+}
       if ioresult=0 then
       begin
          // write
          mfh.file_type:='MHDR';
          mfh.vers_code:=100;
          write(f,mfh);
       end
       else
          begin
             {$I-}
                rewrite(f);
             {$I+}
             if ioresult=0 then
             begin
                // write
                mfh.file_type:='MHDR';
                mfh.vers_code:=100;
                write(f,mfh);
             end
             else
                showmessage('Unable to create file!');
          end;
    end;
    tmy_file_header is my first record, but i must to create more records and write them all to one file, without removing/corrupting previous data from/in file.

    If i write another:
    Code:
    f: file of other_record;
    then Delphi displays error: "Incompatible types: first record and other record"

    Creating every time new F type is not good way.
    How to solve this problem? Any ideas?

    btw: i just started learning using delphi "record and type".

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Saving many records to file

    Have a look at BlockRead and Blockwrite. They allow you to write records of anysize to a file.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Saving many records to file

    Thanks for reply.
    I decided to use streams. I think they solved my problem.

    Code:
    type
       tmy_header=record
          f_type:array[0..3] of char;
          codez:word;
       end;
    
       tmy_header2=record
          typez:array[0..3] of char;
          sizez:longword;
    end;
    
     mh: tmy_header
      cht: tmy_header2;
    
      fname: string; // filename
      fstream: tfilestream;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
       fname:='c:\data.dat';
       fstream:=tfilestream.Create(fname, fmCreate or fmOpenWrite or fmShareExclusive);
       try
          mh.f_type:='MHDR';
          mh.codez:=100;
    
          cht.typez:='RDHM';
          cht.sizez:=255;
    
          fstream.Write(mh,sizeof(mh));
          fstream.Write(cht,sizeof(cht));
    
       finally
          fstream.Free;
    end;
    Now the data is correctly in my file, i will try to write more and look how it will work.I have original editor for this file, so i can test with this also, how it loads my file after i create it with my program. Until now it loads my created file OK.
    If i have any problems with this, then i will let u know.

    Thanks so far

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •