Link to home
Start Free TrialLog in
Avatar of Gwena
Gwena

asked on

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...
Avatar of Gwena
Gwena

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Let's see what Gwena's gonna say :)
Avatar of Gwena

ASKER

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 ;-)









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=-..
Avatar of Gwena

ASKER

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?

Avatar of Gwena

ASKER

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 :-)

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.