Author Topic: How do I search for a string in a fileobject/text file and get its line number?  (Read 717 times)

Example:

I want to find 4 in this list:

1
2
3
test
4
5

and then get its line number (5)

Untested
Code: [Select]
%file = new FileObject();
%file.openForRead("file.txt");
while(!%file.isEoF())
{
%line = %file.readLine();
if(%line $= "4")
break;
%linesRead++;
}
%file.close();
%file.delete();
Then check the value of %linesRead

thanks, did I make it into function form properly:

Code: [Select]
function searchInTextFile(%file,%string)
{
%file = new FileObject();
%file.openForRead(%path);
while(!%file.isEoF())
{
%line = %file.readLine();
if(%line $= %string)
{
%file.close();
%file.delete();
return %linesRead;
}
%linesRead++;
}
%file.close();
%file.delete();
}

EDIT: I redid the syntax
« Last Edit: December 28, 2011, 02:13:36 PM by Axolotl »

thanks, did I make it into function form properly:

Code: [Select]
function searchInTextFile(%file,%string)
{
%file = new FileObject();
%file.openForRead(%path);
while(!%file.isEoF())
{
%line = %file.readLine();
if(%line $= %string)
{
%file.close();
%file.delete();
return %linesRead;
}
%linesRead++;
}
%file.close();
%file.delete();
}

EDIT: I redid the syntax
You must either define %path or put the path.

You must either define %path or put the path.
oops forgot to set %file to %path lol