BufDataSet - LoadFromFile problem lazarus / 63-Database ---------------------------------------------------------------------- HappyLarry · 2016-08-08 08:51 In BufDataSet, I can not get LoadFromFile to work. I have tried both Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      T.LoadFromFile('Names.bds'); and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i           T.Edit;     T.LoadFromFile('Names.bds');     T.Post;but neither shows the record. All help gratefully received. Here is my test program Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Loadtest;uses  db,BufDataset; var  T:TBufDataSet; begin     //Create a dataset, add a record and save it     T:=TBufDataset.Create(nil);     T.FieldDefs.Add('Name',ftString,20);     T.CreateDataset;     T.Append;     T.Fields[0].value:='Fred';     T.Post;      writeln('Before Save :',T.Fields.FieldByName('Name').AsString);      T.SaveToFile('Names.bds');     T.Free;      //Create a dataset, load the file and show it     T:=TBufDataset.Create(nil);     T.FieldDefs.Add('Name',ftString,20);     T.CreateDataset;     //T.Edit;     T.LoadFromFile('Names.bds');     //T.Post;     writeln('After Load :',T.Fields.FieldByName('Name').AsString);      T.free;    writeln('------ done ----------');   readln;end. ---------------------------------------------------------------------- howardpc · 2016-08-08 09:06 LoadFromFile creates the data set. All you want after saving the data set to file is this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //Create a dataset, load the file and show it  T:=TBufDataset.Create(nil);  T.LoadFromFile('Names.bds');  writeln('After Load :',T.Fields.FieldByName('Name').AsString);  T.free; ---------------------------------------------------------------------- ttomas · 2016-08-08 09:06 This work OK Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   //Create a dataset, load the file and show it  T:=TBufDataset.Create(nil);//---->  T.FieldDefs.Add('Name',ftString,20);   //---->  T.CreateDataset;  //T.Edit;  T.LoadFromFile('Names.bds'); ---------------------------------------------------------------------- wp · 2016-08-08 09:27 Or do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i T := TBufDataset.Create(nil);T.FileName := 'Names.bds';T.Open;Having specified the filename will cause automatic saving of the file if the dataset is closed later. ---------------------------------------------------------------------- HappyLarry · 2016-08-08 12:22 @ All Great answers - Thanks Another problem: I have tried to write a procedure to load a text file. The text file record structure is (String, Integer, Real). I have written: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure LoadTextFile(Filename:string; var T:TBufDataSet);var  MyFile:Text;  C1:string;  P1:integer;  A1:real; begin      Assignfile(MyFile,FileName);      Reset(MyFile);      While not System.EOF(MyFile) do      begin        readln(MyFile,C1);        readln(MyFile,P1);        readln(MyFile,A1);        T.AppendRecord([C1, P1, A1]);      end;      CloseFile(MyFile);end;  This works but is not generally usable, because it relies on the record structure being(String, Integer, Real), Is there a way of writing it so that it works whatever the record structure, for instance if the record structure was (String, Integer, Integer, String);? If it helps, here is the full program code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Loadtest;uses  db,BufDataset; var  T:TBufDataSet; procedure SaveTextFile(Filename:string; T:TBufDataSet);var  j:integer;  MyFile:text;begin  Assignfile(MyFile,FileName);  Rewrite(MyFile);  T.First;  While not T.EOF do  begin    For j:= 0 to T.FieldCount-1 do    begin        writeln(MyFile,T.Fields[j].Value);    end;    T.next;  end;  CloseFile(MyFile);end; procedure LoadTextFile(Filename:string; var T:TBufDataSet);var  MyFile:Text;  C1:string;  P1:integer;  A1:real; begin      Assignfile(MyFile,FileName);      Reset(MyFile);    While not System.EOF(MyFile) do      begin        readln(MyFile,C1);        readln(MyFile,P1);        readln(MyFile,A1);        T.AppendRecord([C1, P1, A1]);      end;    CloseFile(MyFile);end;    begin     //Create a dataset,     T:=TBufDataset.Create(nil);     T.FieldDefs.Add('C',ftString,20);     T.FieldDefs.Add('P',ftInteger);     T.FieldDefs.Add('A',ftFloat);    //add a record     T.CreateDataset;     T.Append;     T.Fields[0].value:='Belgium';     T.Fields[1].value:=11;     T.Fields[2].value:=32.5;     T.Post;      //Save     writeln('Before Save :',T.Fields.FieldByName('C').AsString);     SaveTextFile('Countries.txt', T);      //Clear Table     T.Delete;   writeln('After Delete :',T.RecordCount,' records');      //Reload     LoadTextFile('Countries.txt',T);     writeln('After Load :',T.Fields.FieldByName('C').AsString);      T.free;    writeln('------ done ----------');   readln;end. ---------------------------------------------------------------------- howardpc · 2016-08-08 12:52 For persistent data saved to a textfile you can organise your file so that the first line saved with SaveToText represents the field structure (fieldname, type, and length). Your LoadTextFile procedure would then parse the first line read as a special composite value whose individual parts enable you to reconstruct the correct TFieldDefs for your BufDataSet. Or you can save this information as a separate schema file which LoadTextFile opens and reads first. Either way, once the correct database structure is determined, LoadTextFile can continue to read individual records using ReadLn, parsing each record into its constituent fields which are appended to the dataset in memory. Note that best practice is to use TextFile (rather than Text) as the file type. If you ever migrate your code to a GUI app, use of Text will most likely produce  unwelcome name clashes with the Text properties of numerous classes.