Hy there.
I have some code where I store data for 365 records (for every day in a year) in delphi, some kind of list as far as I understood (a good man!!! Sir Rufo on stackoverflow helped me with that part of code, writing whole example for me! Thanks for that.), using procedure initdata, procedure storecurrent, procedure loadcurrent, and procedure savedata. So far everything as he made works, but he left me the savedata procedure empty. So far, for a single memo I've successfuly saved the text to a file using file of, assign and then rewrite with write (file, var of record. But here with this array[1..365] of record I got stuck. Can'f find a way to tell the Write(file, var of record[x]) what to acctually write into file. Since I'm not really familiar with those initdata and savedata procedures, and can't get to find anything on google :S

Here's his complete code (as mine is a lot longer, and has a lot added already, having 8 elements in each record, etc....]

unit RecordEdit_ViewU;
interface
uses
  SysUtils,
  Controls, Forms, Dialogs, StdCtrls;
type
  TPerson = record
    Firstname : string[50]; // shortstring !!
    Lastname : string[50];  // shortstring !!
  end;
  TRecordEdit_View = class( TForm )
    Current_Edit : TEdit;
    Data_Firstname_Edit : TEdit;
    Data_Lastname_Edit : TEdit;
    Data_Prev_Button : TButton;
    Data_Next_Button : TButton;
    Data_Save_Button : TButton;
    procedure FormCreate( Sender : TObject );
    procedure Current_EditChange( Sender : TObject );
    procedure Data_Prev_ButtonClick( Sender : TObject );
    procedure Data_Next_ButtonClick( Sender : TObject );
    procedure Data_Save_ButtonClick( Sender : TObject );
  private
    FData :    array [1 .. 365] of TPerson;
    FCurrent : Integer;
    procedure SetCurrent( const Value : Integer );
    procedure InitData;
    procedure StoreCurrent;
    procedure LoadCurrent;
    procedure SaveData;
  public
    property Current : Integer read FCurrent write SetCurrent;
  end;
var
  RecordEdit_View : TRecordEdit_View;
implementation
{$R *.dfm}

procedure TRecordEdit_View.Current_EditChange( Sender : TObject );
begin
  Current := StrToIntDef( Current_Edit.Text, 0 ); // convert text to integer
end;
procedure TRecordEdit_View.Data_Next_ButtonClick( Sender : TObject );
begin
  Current := Current + 1; // next record
end;
procedure TRecordEdit_View.Data_Prev_ButtonClick( Sender : TObject );
begin
  Current := Current - 1; // prev record
end;
procedure TRecordEdit_View.Data_Save_ButtonClick( Sender : TObject );
begin
  SaveData;
end;
procedure TRecordEdit_View.FormCreate( Sender : TObject );
begin
  InitData;
end;
procedure TRecordEdit_View.InitData;
begin
  FCurrent := Low( FData ); // first record
  LoadCurrent;              // load data from record
end;
procedure TRecordEdit_View.LoadCurrent;
begin
  // Data from record to controls
  Data_Firstname_Edit.Text := FData[Current].Firstname;
  Data_Lastname_Edit.Text  := FData[Current].Lastname;
  // Update the Current-Edit
  Current_Edit.Text := IntToStr( Current );
end;
procedure TRecordEdit_View.SaveData;
begin
  ShowMessage( 'Needs to be implemented!' );
end;
procedure TRecordEdit_View.SetCurrent( const Value : Integer );
begin
  // check, if we have a change and if we can change to the new index
  if ( Value <> Current ) and ( Value >= Low( FData ) ) and ( Value <= High( FData ) )
  then
    begin
      StoreCurrent;      // store data from controls
      FCurrent := Value; // change current index
      LoadCurrent;       // load data from record
    end;
end;
procedure TRecordEdit_View.StoreCurrent;
begin
  // Data from controls to record
  FData[Current].Firstname := Data_Firstname_Edit.Text;
  FData[Current].Lastname  := Data_Lastname_Edit.Text;
end;
end.

There, where I ged ShowMessage('Needs to be implemented!') I really don't know what to do. I mean, I've tryed what I thought would work but no luck so far. Any hints, on what to look for, so I'd figure this out? Because more than make this work I'd like to understand how and why it works then (of course, I hope I'll get it work as well.. :D ). Then, I need to also read that file in another form, but I think I'll get it done once knowing how to at least write it (since for a single record or array, without storing that file in delphi, I successfully managed to do that!)

And yes, I am kind of beginner still... but learning quick ;)

Thanks. :)

If you had looked a little closer to this forum, you'd have seen that the two questions before yours are about similar issues. Check them out, perhaps it will get you started in the right direction.

Hello, pritaeas. Thanks for your reply.
I had looked in them, (if we mean the same articles: This and this, but I've read something more similar in this one (and thanks for removing duplicate, I just rather started new topic since it was too old and thought later that some don't like to pull old-ones up) ).
All I figured out so far is, that I might be missing the blockread..? But I'm not sure if that's the case. So far I know that to save the data to a file I have to step through all records from 1 to 365 and write them down. I thought that the "for" loop would do the trick, and tried several codes, but no luck.

I have there

    procedure TWriteForm.SaveData;
        var I: Integer;
    begin
        CreateDir ('C:/SHMChallenge');
        AssignFile(FileVar, 'C:/SHMChallenge/InfoList.shm');
        ReWrite(FileVar);
 //     for i := 1 to 365 do
 //     begin
      Days[i].ShortName := EditShortName.Text;
      Days[i].Name := EditName.Text;
      Days[i].VideoCode := EditVideoCode.Text;
      Days[i].Lyric := MemoLyric1.Text;
      Write(FileVar, Days[i]);
//      end;
      CloseFile(FileVar);
      ShowMessage( 'Done.' );
    end;

but that obviously doesn't work... :/ Am I completely off the road...?

Oh, and that's my code, so vars, Record names and form name is different than above.
i for integer is on purpose NOT current, as I think I can't use the same integer without declaring it again, so I just used another.. is that ok..?
Thanks!

Well, I keep getting empty file recreated.
I even tryed to fix a variable I, to store just one record, but seems like my array of record doesn't get to be written at all... :/
I'll try a few other stuff that came on my mind, and post results, if not getting any, may I post you my whole code to be checked, please? Thank you!

I forgot to remove 3 times // in the beginning of the sentence (for, begin & end), they do run in my code..
Anyway, just ifgured out that this code above does something: whatever I fill in any of the record, pressing the button copies that string to all other records, a.k.a. if I fill in first record with "testname", "testlongname" etc, pressing save button will fill all of the 365 records with that same data, but still leaving the file itself (appended) empty 0 bytes capacity file, just recreated... ://
Should I post the whole code up, or you know the catch..?

Ok. Thank you.

Ps: Most of var's are written with slovenian words, as I'm from slovenia, so if there's any problem, contact me, please.
Thank you a lot! I'll sepparate source and upload it on.

Here. I took time and write down the comments almost everywhere in the code, so you really know where is what without exploring too much. :)
I also posted all the problems and questions within the comments itself, so, if you could take time to read it (it's not that much however) and answer/help on ANY of them, I will be really grateful. Looking forward to your answers soon. Thank you again! :)

Ps: The name of the file/program is the name of a main exe file, within which are all three forms, Main, Reader and Writer. So far I only appended the writter pass in zip, since there's help needed, and also because the other forms are not yet completed (without the file, which needs to be written by a Writer form, I can predict working of the Main unit, but can't do and continue creating it too deep, since I need to costantly check the behaviour of the functions). However I do promise, that I'll upload the final (Demo) version of it, once finished and working! ;)

Thanks again for helping..

Marc

Edited 4 Years Ago by cucolino: File attachment dismissed in first post..

UPDATE:

Forget about my questions above. I'm an idiot. :D

I just must tell you: I'm really embarrassed of myself right now. Because it took me 2 days for me, an idiot, to figure out that leaving one of the timers on the other form enabled is my acctual problem (I had it before this code, for automaticaly storing changes in a file, when testing other stuff): it had rewrote my file every second, and therefore I always got an empty file instead of what I wanted to have in there!!! :S So, the mistake was my sloppiness and nothing else! :( As it seems, though I am only a beginner, I did find the right code only shortly after he wrote me that part, and the for loop was/is right, only that in every loop after Begin and before setting the values of the records I needed Current:=i; as well, and that now works GREAT! :)

The code above in a zip file will therefore work, (almost), since I deleted all the rest out of the project, so... nice job, I know -.-

Thank you anyway for taking your time, now I'm on it to go further, and as promised, will post the finished example, maybe just to get an opinion from you, experts, on how did I do one of my first projects... ;)

This article has been dead for over six months. Start a new discussion instead.