Very Difficult Coding Problem

Started by
3 comments, last by DragonPhinn 22 years, 8 months ago
Ok, this is my problem: I have a TStringGrid on a form with 100x100 cells - I need to add these cells into a text file. I do it using a nested for loop: str := ''''; for y := 0 to 99 do begin for x := 0 to 99 do begin str := str+StringGrid1.Cells[x,y]+'',''; {adds a comma after} {each entry for } {easy parsing} ProgressBar1.StepIt; end; memo1.lines.add(str); str := ''''; end; For those of you who don''t understand how it works it basically iterates through all the positions in the StringGrid1, adds them to a str (followed by a comma). After every x (row) adds the string to a memo component, and the resets the string. That way the memo1 looks just like the StringGrid, except with commas. Here''s the problem: it doesn''t add all the lines of the StringGrid. So I added some debug lines. I made a debug window that had a TMemo component on it and I made the for loops add a line to it after every iteration. For example, after the str := str+StringGrid1.Cells[x,y]+'',''; I added Form8.Memo1.Lines.Add(''Processed X-''+inttostr(x)+'' Y-''+inttostr(y)); I also added StatusBar1.Panels[0].Text := ''X: ''+inttostr(x); StatusBar1.Panels[1].Text := ''Y: ''+inttostr(y); And then I displayed the memo1 contents - and used some code to count the lines. Here is the crazy output for each of these Debug methods: 1 - The Form8 says that the processing was completed at: X-78, Y-22. I ran it again, and then after the X-78, Y-22 it went back to X-0 y-0. Wierd huh? 2 - The Status Bars say that it went to X:99 Y:99. 3 - There are 79 lines on the saver component. ---------- So then I tried a different coding technique. I used a listbox, and a memo2. memo1.Clear; {<-New memo component} listbox2.Clear; for w := 0 to 99 do listbox2.items.addstrings(stringgrid1.rows[w]); z := 0; for y := 0 to 99 do begin for x := 0 to 99 do begin str := str+ListBox2.Items.Strings[z]+'',''; z := z+1; ProgressBar1.Stepit; Form8.Memo1.Lines.Add(''Processed X-''+inttostr(x)+'' Y-''+inttostr(y)); end; memo1.lines.Add(str); str := ''''; end; memo1.visible := true; listbox2.visible := true; saver.Lines.addstrings(memo1.lines); {TMemo} save1.InitialDir := ExtractFileDir(save1.filename); {TSaveDialog} saver.Lines.SaveToFile(save1.filename); saver.Visible := true; end; progressbar1.Position := 0; Form8.Memo1.ScrollBars := ssBoth; Form8.Show; end; And these are the whacky return values: Form8.Memo -> Same as the first method - 78,22 Saver1.Count -> 78 lines Memo1.Count -> 79 Lines HERES THE BIG ONE 10,000 entries into the ListBox, which means the problem lies with the transfer into the memo component. ----------- Some thoughts of mine: Memo1.Lines had a maximum entry length. Each line can hold 1074 characters - my largest lines (if all read ''[None]'' are in the upper 800''s. So it shoud fit with room to spare. Something is messed up with the TStringGrid - nope, because it got all the information to the listbox. All 10,000 entries. Nested for loops? I used the same format to initialize the StringGrid as well as about 100 otehr places in the program. All of which operate fine. Well, I turn to you guys to see what you can do. -DragonPhinn of Otakujin Webmaster of http://www.thedragonlair.50megs.com PS Source will be distributed upon request. I''d rather not though.
Advertisement
I''ve got one word for you "HyperString"

HyperString v5.0 now offers over 400 efficient string and system routines designed to complement the versatile new 32-bit long dynamic string type in Borland''s Delphi. Extensive documentation in WinHelp format can be easily integrated into the Delphi IDE by following the simple instructions provided.

HyperString has everthing you need if you want to store your game data in text format.

http://efd.home.mindspring.com/hyperstr.htm



[ Michael Wilson | turbo sys-op | turbo.gamedev.net ]
[ Michael Wilson | turbo sys-op | turbo.gamedev.net ]
From your code and results, I guess you're under a Win9x system.

Your str:='' is wrongly placed (should be in the first loop, after the "for y"), so your str grows, and grows and grows, while on Win9x the TMemo only allows up to 64 kB of data (and even 32 kB in some circumstances), it is filled up before the end of the grid.
Even without this, if each line is 800 chars or more, 64 kB/800 = approx 80, so your memo can store approx 80 rows... rings a bell?

You may alternatively use StringGrid.Rows[y].CommaText to extract the grid content on a per-line basis (won't use ";" as separator, but will be safe for special characters and allows writing rows directly).

Also note that the TRichEdit does not have this limitation under Win9x (the TMemo is not limited under NT).

---
Eric Grange
http://glscene.org

Edited by - Eric Grange on August 7, 2001 3:50:27 AM
---Eric Grangehttp://glscene.org
Hi,

Question : what are you trying to accomplish ? - Planning on using the SaveToFile to create a diskfile ?

If so, better ways are

- working with flatfile TClientDatasets : very easy file import/export and works like a table.

- working directly with files like :
var
OutputFile : TextFile;
...
AssignFile (OutputFile, ''C:\MillionDollarGame\GameData.Dat'');
Rewrite(OutputFile);
...
WriteLn(OutputFile,OutputStr);
CloseFile(OutputFile);


Greetz,

Eddy Sterckx
You guys are 3l33t. Thanks.

This topic is closed to new replies.

Advertisement