SRL Archive - Help and Support

Reading a line from a .txt file.


R0b0t1

2007-08-21 15:56:48

I have a file with 144 lines in it, and a string on each line. I need to read these strings, and out put it with a writeln. I don't think ReadFileString will work for this.


Helpz?



EDIT: If you wanted to know, its for an Array object in Java script… So I'm gonna use a For loop to create it, and the line of the file it reads will be its array number. (I am going to start the array at one, so the first object will be blank).

Bobarkinator

2007-08-21 20:29:55

Does every line have the same amount of characters?

R0b0t1

2007-08-21 20:32:19

No, sadly. Thats one reason why I realized why ReadFileString would not work.

Bobarkinator

2007-08-21 20:39:41

If you upload it to the internet i can do it, I don't know of any other way.

R0b0t1

2007-08-21 20:48:01

Sure, how would I get it from the internet? I could just make it an attachment to a post.

Its only for a single use though. No huge ideas please :p.

pwnaz0r

2007-08-21 21:09:20

Freewebs. They have file attaching which you can view. Or


Procedure ReadFString(var S : string; FileNum : integer);
var
tmpStr : string;
S2 : Integer;
Rslt : Boolean;
begin

S2 := OpenFile({FilePath}'C:\text.txt',{Shared}false);

Rslt := ReadFileString(S2,tmpStr,1000);

tmpStr := Trim(tmpStr);



Writeln(tmpStr);

end;

end;

R0b0t1

2007-08-22 14:05:42

No. Sorry :\ wouldn't work. I need to get each line of the file and return it :D.


I remember a tutorial on getting stuff from the web, but not single lines…

~alex~

2007-08-22 14:14:21

someone made a read fie line thing… hang on till i try find it.

edit: Got it. By lordgreggreg


function readFileLine(GregFile:Integer):String;//By Greg Hendrickson!
var
s,bigs:String;
begin
while not EndOfFile (GregFile) do
begin
ReadFileString(GregFile,s,1);
bigs:=bigs+s;
if(s=#10) then
begin
result:=bigs;
exit;
end;
writeln(bigs);
end;
end;

Sumilion

2007-08-22 14:26:43

Got this from a site :

function explode(cDelimiter,  sValue : string; iCount : integer) : TStringArray;
var
s: string;
i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin
Inc(i);
SetLength(result, i);
p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
begin
Result[i - 1] := copy(s,0,p-1);
s := copy(s,p + length(cDelimiter),length(s));
end else
begin
result[i - 1] := s;
s := '';
end;
end;
end;

Now use it like this :

  FileNumber := OpenFile('PATH', False);
ReadFileString(FileNumber, FileString, FileSize(FileNumber));
FileStrings := Explode(Chr(13), FileString, 0);
for i := 0 to Length(FileStrings) - 1 do
Writeln(FileStrings[i]);

R0b0t1

2007-08-22 17:28:19

Thanks you. Although, Sumillion, do you always have to be smarter than everyone :p?


EDIT: Well, smarter if it compiled…


program New;

Var I, FileNumber: Integer;
Var FileStrings, FileString: String;

function explode(cDelimiter, sValue : string; iCount : integer) : TStringArray;
var
s: string;
i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin
Inc(i);
SetLength(result, i);
p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
begin
Result[i - 1] := copy(s,0,p-1);
s := copy(s,p + length(cDelimiter),length(s));
end else
begin
result[i - 1] := s;
s := '';
end;
end;
end;


begin
FileNumber := OpenFile('C:\Documents and Settings\Sid.KAY\Desktop\worldprefix.txt', False);
ReadFileString(FileNumber, FileString, FileSize(FileNumber));
FileStrings := Explode(Chr(13), FileString, 0);
For i:= 0 to Length(FileStrings) - 1 do
Writeln('Worlds['IntToStr(I+1)+'] = AddWorldInfo('+IntToStr(I+1)+
', "'+FileStrings[I]+'", "", "")');

end.


Failed when compiling
Line 33: [Error] (33:47): Type mismatch in script

TOB

2007-08-22 17:47:52

Ron and Moparisthebest both made explode type functions btw.

Sumilion

2007-08-22 20:05:21

From: r0b0t1
Thanks you. Although, Sumillion, do you always have to be smarter than everyone :p?


EDIT: Well, smarter if it compiled…

ermm yah! :)

the strings var must be TStringArray silly…

R0b0t1

2007-08-23 13:34:52

*Speechless*


program New;

Var
I, FileNumber: Integer;
FileString: String;
FileStrings: Array [0..144] of String;

function explode(cDelimiter, sValue : string; iCount : integer) : TStringArray;
var
s: string;
i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
begin
Inc(i);
SetLength(result, i);
p := pos(cDelimiter,s);
if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
begin
Result[i - 1] := copy(s,0,p-1);
s := copy(s,p + length(cDelimiter),length(s));
end else
begin
result[i - 1] := s;
s := '';
end;
end;
end;


begin
FileNumber := OpenFile('C:\Documents and Settings\Sid.KAY\Desktop\worldprefix.txt', False);
ReadFileString(FileNumber, FileString, FileSize(FileNumber));
FileStrings := Explode(Chr(13), FileString, 0);
For i:= 0 to Length(FileStrings) - 1 do
Writeln('Worlds['+IntToStr(I+1)+'] = AddWorldInfo('+IntToStr(I+1)+
', "'+FileStrings[i]+'", "", "")');

end.

Type mismatch line 35…

Bobarkinator

2007-08-23 20:14:18

Change it to

var
FileStrings: TStringArray;

the scar noob

2007-08-26 17:21:14

a line eh?
let's say the line is:
[PHP]help me out you freaking nubs![/PHP]

you can use "between" and place two //

[PHP]<help me out you freaking nubs!>[/PHP]
then getpage, and let it read the text between the <> (or some other charls like: / * -)

I dunno if i explained it clear enough, plz PM me if you don't udnerstand it..

TSN.

EDIT: between does:
returns a string by checking between 2 substrings in a string, for example:

function between(s1, s2, s: string): string;

string: 'I love the new SRL 4 include.'
substring#1: 'new '
substring#2: ' include .'

it will return '4' as a string :)

Explanation:
It checks for the string in the sentence between the 2 substrings and it also results the result as a string :)

R0b0t1

2007-08-26 21:42:01

ALL WORSHIP THE 1337n33s THAT IS THE BARKER!


Bob Barker Bob Barker show us how to get down. D-O-W-N show us how to get down.

Bobarkinator

2007-08-26 22:44:36

From: r0b0t1
ALL WORSHIP THE 1337n33s THAT IS THE BARKER!


Bob Barker Bob Barker show us how to get down. D-O-W-N show us how to get down.


Um…Ok…:spot: :spot: (had to throw in a dance cause he asked :p)