Getting a specific line from a file in Haxe

115 views
Skip to first unread message

Andy Green

unread,
Nov 12, 2012, 10:34:20 PM11/12/12
to haxe...@googlegroups.com
In Haxe, is it possible to obtain a specific line from a file (using one of the standard classes, such as Sys)?

static function getMethodName(lineNumber, fileName){
//Is there a method that does this?
}

j...@justinfront.net

unread,
Nov 12, 2012, 11:41:46 PM11/12/12
to haxe...@googlegroups.com
You can with trace...
And also with macros but unsure of details.

David Peek

unread,
Nov 13, 2012, 12:28:53 AM11/13/12
to haxe...@googlegroups.com
static function readLine(path:String, line:Int):String
{
if (line < 1) throw "Can't read line " + line;
var out = sys.io.File.read(path);
for (i in 0...line - 1) out.readLine();
var line = out.readLine();
out.close();
return line;
}

Mihail Ivanchev

unread,
Nov 13, 2012, 3:02:36 AM11/13/12
to haxe...@googlegroups.com
A bit hacky and slow, but quick:

File.getContent(path).split("\n")[lineNumber - 1];

I am using this code in exactly the same case as you, processing CTAGs info and generating code stubs.

Regards,
Mihail

Andy Green

unread,
Nov 13, 2012, 9:37:58 AM11/13/12
to haxe...@googlegroups.com
Is it really possible to print the lines from a file using the trace method alone? I don't see anything in the documentation that suggests that.

Mihail Ivanchev

unread,
Nov 13, 2012, 9:40:55 AM11/13/12
to haxe...@googlegroups.com
It's not possible.

j...@justinfront.net

unread,
Nov 13, 2012, 10:07:10 AM11/13/12
to haxe...@googlegroups.com
Sorry Mihail your wrong, it is possible I have done it lots of times, it was in the first haxe book, but never really thought about it.
Just double checked with js target... 

Read the page carefully ( especially the last part ) and then add this to your Main class....


    static function traceComment( comment : String, ?pos : haxe.PosInfos )
    {
        haxe.Log.trace( comment + " :: Line no " + pos.lineNumber );
    }


Then test...


96        Main.traceComment( 'this is line 96' );

It will trace out...

"this is line 96 :: Line no 96"

Best 

Justin


On 13 Nov 2012, at 14:40, Mihail Ivanchev wrote:

It's not possible.

Mihail Ivanchev

unread,
Nov 13, 2012, 10:15:05 AM11/13/12
to haxe...@googlegroups.com
Oh yeah, Justin, it's possible to print the line of the trace in the source file, of course, you're 100% correct, but it's not possible to load an external file, navigate to a line, and print that line:

trace("myfile.txt", 75);

j...@justinfront.net

unread,
Nov 13, 2012, 10:33:57 AM11/13/12
to haxe...@googlegroups.com
Oh sorry missed the meaning of the question... oops.

Well you need to load it in and have a callback for loaded, and put the characters in a string then something like fileString.split('\n').[75];   or \r\n or \r.

I have parsed CSV files in haxe flash lots of times.
Reply all
Reply to author
Forward
0 new messages