?
Solved

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

Posted on 1999-07-18
9
Medium Priority
?
210 Views
Last Modified: 2010-04-06
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...
0
Comment
Question by:Gwena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Author Comment

by:Gwena
ID: 1389439
Edited text of question.
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 240 total points
ID: 1389440
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1389441
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:viktornet
ID: 1389442
Let's see what Gwena's gonna say :)
0
 
LVL 5

Author Comment

by:Gwena
ID: 1389443
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1389444
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
 
LVL 5

Author Comment

by:Gwena
ID: 1389445
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
 
LVL 5

Author Comment

by:Gwena
ID: 1389446
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1389447
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

718 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