Author Topic: Getting the line after a line in a file  (Read 720 times)

I want to get the line that comes after a line in a file
so say I have a file that records chat(IE everything that is said)
Hi
sup
bricks are fun
OMG SRSLY?

now, if someone says something that is in the file, I want to grab the line after it, IE
if they said Hi, it would return as sup

How would I do this?
I can record to the file, but when I try to grab the right line or check if someone said something in the file, everything goes horribly wrong.(i.e returns a random line, or returns lines even when there isnt a match ect. ect.)

Code: [Select]
%targetLine = "sup";
%file = new FileObject();
%file.openForRead("yourFile.txt");
while(!%file.isEOF())
{
   %line = %file.readLine();
   if(%line $= %targetLine)
      %return = %file.readLine();
}
%file.delete();
return %return;

Would return "bricks are fun"

If you're gunna be searching through chat logs, you should add a break in the loop after you got the next line.

Code: [Select]
%targetLine = "sup";
%file = new FileObject();
%file.openForRead("yourFile.txt");
while(!%file.isEOF())
{
   %line = %file.readLine();
   if(%line $= %targetLine)
      %return = %file.readLine();
}
%file.delete();
return %return;

Would return "bricks are fun"
Thank you!