Can you help me get streamed data from the net into an array?

I am writing some code that pulls data from a small file on my website
http://www.gurlpages.com/computer/gwena/testdata.txt
I found a free component package by a guy named Hoerstemeier
that seems to work ok but all I can manage to do is get the data into a Tmemo box :(
I need to get this data into an array where I can do something with it.... here is the code
I have written that manages to get the data into the memo box

unit httpmain;

interface


uses
  winprocs,
  wintypes,
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  tcpip;


type

TForm1 = class(TForm)
  HTTP: T_HTTP;
  Memo: TMemo;
  btn_doit: TButton;
  procedure btn_doitClick(Sender: TObject);
end;
var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.btn_doitClick(Sender: TObject);

begin
  http.url:= 'http://www.gurlpages.com/computer/gwena/testdata.txt';
  http.Authorization:='';
  http.action;
  memo.lines.loadfromstream(http.stream);
  end;

end.


----------------------------

So how in the world can you get this 'stream' data into a
array?   And by the way how can you get the lines of text from a memo box into a string or an array??

I compiled a tiny exe file that pulls the testdata.txt file off my site and displays it .. it is here if you need to see it ...  

http://www.gurlpages.com/computer/gwena/images/getdata.exe

By the way I'm using Delphi 2 standard to write this...
LVL 5
GwenaAsked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
Well, its already putting them into an array. The lines property of the TMemo is a StringList - essentially an array of strings.

You can either just pull the strings out of the TMemo, or write your own component which knows how to read a stream in. Personally I would pick the former!

Cheers,

Raymond.

0
 
GwenaAuthor Commented:
Edited text of question.
0
 
rwilson032697Commented:
Actually, to add some more to this, notice that it is the lines property doing the streaming? This means you can do this:

var
  TheStrings : TStringList;

...
  TheStrings.loadfromstream(http.stream);
...

Cheers,

Raymond.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
viktornetCommented:
Let's see what Gwena's gonna say :)
0
 
GwenaAuthor Commented:
Very Cool!  :-)

OK.. I now have the code working just fine...I can use the data by doing the following...

var
WebData: Array[0..600] of string;

then later I....

memo.lines.loadfromstream(http.stream);
for i := 0 to memo.lines.count -1  do
  WebData[i] := memo.lines[i];

Now I have the data I want to play with in my WebData string array :-)


When I try to do this without using a Tmemo I still have probs :(

var
TheStrings: TStringList;

Then later...

TheStrings.loadfromstream(http.stream);


When I do this I get an error that says 'access violation' ???
If the (TheStrings: TstringList;) is like an array of strings then do you think
that I am somehow attempting to access a nonexistant element of the array?
How do I set the array's size?
do I access elements of this array like this..
TheStrings[x]  or does it have a property like the 'lines' in the Tmemo  ??

I'm partway there...but still  a bit clueless ;-)









0
 
viktornetCommented:
you can do something like this....

WebData: Array[0..600] of string[50];

Move((http.stream as TMemoryStream).Memory^, WebData^, stream.size);

something like that might do it :))

..-=ViKtOr=-..
0
 
GwenaAuthor Commented:
I looked through all the help files about Tstringlist and tried the following which seems to work


var
 TheStrings : TStringList;


then later

TheStrings := TStringList.Create;
TheStrings.loadfromstream(http.stream);

for i := 0 to 2 do
  webdata[i] := TheStrings[i];
  TheStrings.Free;


This works...but is it correct??? or will it lead to bugs later on?

plus...how do I determine how many elements there are in the TheStrings array?
I used  memo.lines.count to find out how many strings had been loaded into the Tmemo .. is there a similar way to determine array size for TheStrings?

0
 
GwenaAuthor Commented:
Ok... I think I have most of my problems solved now :)
I just used    TheStrings.count  to get the number of strings in the array...  DUH... (wake up Gwena)
Thanx guys... now I will grade the answer :-)

0
 
rwilson032697Commented:
Gwena: Yes, you need to create the TStringlist - I didn't do that as I was really showing a skeleton with some context - sorry about that!

Of course, you can always jsut use the stringlist and not move the strings to an array (as strings (assuming you are using $H+ or ANSIStrings) are actually just pointers anyway, so an array of string is very close in structure to a stringlist).

Cheers,

Raymond.

0
All Courses

From novice to tech pro — start learning today.