[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

How to Save the contents of a TStringgrid to a textfile?

Hi Folks!

Simply put: if my stringgrid has 3 cols and 3 rows and each cell houses 1 character and this looks like:

A B C
D E F
G H I

How can I edit this stringgrid at runtime and save this to a text file so that I can load it next time > Suppose I run the app with the above mentioned 9 characters and I decide to alter it into:

A B C
D X F
G H I

(notice the X instead of the E :)

How do I make sure I can edit it at run time and save it to a text file?

Regards Peter

Providing the copy&paste stuff does the job, hence I'm already used to that 'mechanism' so why change it? ;)



0
PeterdeB
Asked:
PeterdeB
1 Solution
 
shaneholmesCommented:
Peter,

I was looking for some info in the newgroups for my work project - and came
upon this. I thought you might been interested in it.

StringGrid SaveToFile & LoadFromFile (using Streams)

Sorry, im still stuck on my work project - its due tommorrow....

shane


TStringGrid has a Rows property which is an array each element of which
is a TStringList. Each TStringList has a Text property which is a cRLF
delimited list of the strings. Save the RowCount property of the
TStringGrid to a file followed by the Text strings of each Rows[]
element. As follows ...

procedure WriteStreamStr(Stream : TStream; Str : string);
{writes a string to the stream}
var
  StrLen : integer;
begin
  {get length of string}
  StrLen := Length(Str);
  {write length of string}
  Stream.WriteBuffer(StrLen, SizeOf(Integer));
  {write characters}
  Stream.Write(Str[1], StrLen);
end;

function ReadStreamStr(Stream : TStream) : string;
{returns a string from the stream}
var
  LenStr : integer;
begin
  Result := '';
  {get length of string}
  Stream.ReadBuffer(LenStr, SizeOf(Integer));
  {set string to get memory}
  SetLength(Result, LenStr);
  {read characters}
  Stream.Read(Result[1], LenStr);
end;

procedure SGSaveToFile(SG : TStringGrid; FPN : string);
var
  FS : TFileStream;
  i : integer;
begin
  FS := TFileStream.Create(FPN, fmCreate);
  try
    {store row count}
    FS.Write(SG.RowCount, SizeOf(integer));
    {store text of each row stringlist}
    for i := 0 to SG.RowCount - 1 do
      WriteStreamStr(FS, SG.Rows[i].Text);
  finally
    FS.Free;
  end;
end;

procedure SGLoadFromFile(SG : TStringGrid; FPN : string);
var
  FS : TFileStream;
  SGRowCount, i : integer;
  RowText : string;
begin
  FS := TFileStream.Create(FPN, fmOpenRead);
  try
    {read & set count of rows}
    FS.Read(SGRowCount, SizeOf(integer));
    SG.RowCount := SGRowCount;
    {read & set each row}
    for i := 0 to SGRowCount - 1 do begin
      RowText := ReadStreamStr(FS);
      SG.Rows[i].Text := RowText;
    end;
  finally
    FS.Free;
  end;
end;

Learn a bit more about a TStringGrid by browsing through the Delphi
help file and the properties of its various objects.
0
 
kretzschmarCommented:
instead of a stream you can also use the ordinary pascal read/write procedures
also possible may the internal componentstreaming (see writeComponent/readComponent)

meikl ;-)
0
 
ZhaawZSoftware DeveloperCommented:
You can save these values just the same way you loaded them (you already have a sollution for that).


object StringGrid1: TStringGrid
  Left = 8
  Top = 8
  Width = 66
  Height = 66
  ColCount = 3
  DefaultColWidth = 20
  DefaultRowHeight = 20
  FixedCols = 0
  RowCount = 3
  FixedRows = 0
  Options = [goVertLine, goHorzLine, goEditing, goAlwaysShowEditor]
  TabOrder = 0
end
object Button1: TButton
  Left = 8
  Top = 80
  Width = 75
  Height = 25
  Caption = 'load'
  TabOrder = 1
  OnClick = Button1Click
end
object Button2: TButton
  Left = 8
  Top = 112
  Width = 75
  Height = 25
  Caption = 'save'
  TabOrder = 2
  OnClick = Button2Click
end


procedure TForm1.Button1Click(Sender: TObject);
var
  f : file;
  arr : array [0..8] of char;
  n : byte;
begin
AssignFile(f, 'd:\test.txt');
FileMode := fmOpenRead;
Reset(f, 1);
BlockRead(f, arr[0], 9);
CloseFile(f);
for n := 0 to 8 do StringGrid1.Cells[n mod 3, n div 3] := arr[n];
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  f : file;
  arr : array [0..8] of char;
  n : byte;
begin
for n := 0 to 8 do arr[n] := StringGrid1.Cells[n mod 3, n div 3][1];
AssignFile(f, 'd:\test.txt');
FileMode := fmOpenWrite;;
Rewrite(f, 1);
BlockWrite(f, arr[0], 9);
CloseFile(f);
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterdeBAuthor Commented:
Hi Folks!

Your code works Zhaawz! Then Shane > I have been studying the TStringgrid but I find it very hard to understand all involved reading and writing from and to textfiles.

For instance > How to modify Zhaawz code sample in order to have it deal with  a stringgrid measuring 20 cols and 20 rows and 400 characters? The save thingie I mean, can't get it to work.

Regards Peter
0
 
shaneholmesCommented:
Thats alright - you got a working example...from Zhaawz.... i wont worry about you now - im so busy with my work project.
0
 
PeterdeBAuthor Commented:
Ok > then I'll post another question to modify the sample. Success with the project you're working on Shane and thanks for all your help!

Regards Peter :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now