?
Solved

How to save the contents of this TStringgrid to a textfile?

Posted on 2005-04-12
4
Medium Priority
?
272 Views
Last Modified: 2010-04-05
Hi Folks!



How to have this baby handle a 20 col and 20 row TStringgrid filled with 400 characters?

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;

It would also be great when you  would comment what you do. (the copy&paste stuff is still my favorite...well ofcourse :)

Regards Peter
0
Comment
Question by:PeterdeB
4 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13763656
Are you trying to save the entire contents of the stringgrid to a file, or performing some calculations first?
You could save each value to a new line of a stringlist and then save the stringlist to a file.
You just seem to be doing some very odd looping through the grid..

Stringlist.SaveToFile('d:\text.txt');
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13763871
procedure TForm1.StringGrid2File(StringGrid: TStringGrid;
                                 FileName: String);
var
  F: TextFile;
  x, y: Integer;
begin
  AssignFile(F, FileName);
  Rewrite(F);
  Writeln(F, StringGrid.ColCount);
  Writeln(F, StringGrid.RowCount);
  for x:=0 to StringGrid.ColCount-1 do
    for y:=0 to StringGrid.RowCount-1 do
      Writeln(F, StringGrid.Cells[x,y]);
  CloseFile(F);
end;

procedure TForm1.File2StringGrid(StringGrid: TStringGrid;
                                 FileName: String);
var
  F: TextFile;
  Tmp, x, y: Integer;
  TmpStr: string;
begin
  AssignFile(F, FileName);
  Reset(F);
  Readln(F, Tmp);
  StringGrid.ColCount:=Tmp;
  Readln(F, Tmp);
  StringGrid.RowCount:=Tmp;
  for x:=0 to StringGrid.ColCount-1 do
    for y:=0 to StringGrid.RowCount-1 do
    begin
      Readln(F, TmpStr);
      StringGrid.Cells[x,y]:=TmpStr;
    end;
  CloseFile(F);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  StringGrid2File(StringGrid1, 'c:\file.txt')
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  File2StringGrid(StringGrid1, 'c:\file.txt')
end;
0
 
LVL 17

Accepted Solution

by:
geobul earned 500 total points
ID: 13764020
Hi,

If the procedure you've posted works then the following should work too:

procedure TForm1.Button1Click(Sender: TObject);
var
  f : file;
  arr : array of char;
  n, len : integer;
begin
  len := StringGrid1.ColCount * StringGrid1.RowCount;
  SetLength(arr, len);
  for n := 0 to len - 1 do arr[n] := StringGrid1.Cells[n mod StringGrid1.ColCount, n div StringGrid1.ColCount][1];
  AssignFile(f, 'd:\test.txt');
  FileMode := fmOpenWrite;;
  Rewrite(f, 1);
  BlockWrite(f, arr[0], len);
  CloseFile(f);
end;

Regards, Geo
0
 

Author Comment

by:PeterdeB
ID: 13765514
Hi Folks!

Tnx for replying first of all.

MikeLittlewood > the purpose is to save the entire TStringgrid contents to a textfile (after modifying some characters). The textfile saved serves as a wordsearch puzzle, so now calculations are done. It just needs to be loaded exactly the same way it was saved in order to maintain the same order. Hope this answers your question.

BlackTigerX and Geobul > I chose Geobuls code, because it meant minimal modifications to my project and you're right Geobul > works perfect!

Regards Peter
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question