Author Topic: [Solved] Need help with File I/O  (Read 1058 times)

Code: [Select]
function evar_getLineCount()
{
%file = new fileObject();
%file.openForRead("Add-Ons/Client_Derp/text.txt");
while(!%file.isEOF())
{
%lineCt++;
echo(%file.readLine());
}
return(%lineCt);
%file.close();
%file.delete();
}

Blockland hangs when I call this function, what did I do wrong?
Text.txt is 359 lines long and has a filesize of 22kb.

Basically what I need to do is this:
Text.txt contains a lot of different lines of text to output to chat. I need to know how to pick a certain line from the file, and output it to the chat.

I have a basic idea on how to do this, but still need help.
« Last Edit: December 15, 2012, 04:19:43 PM by Evar678 »

Get rid of the last 3 lines of code, and add this to the end:

return %lineCt;

I think the catch is because you did return(stuff) instead of return stuff;

Get rid of the last 3 lines of code, and add this to the end:

return %lineCt;
Trying that now.
I think the catch is because you did return(stuff) instead of return stuff;
Might be the issue. One moment.
« Last Edit: December 15, 2012, 03:58:15 PM by Evar678 »

Nope, didn't work, blockland still becomes non responsive.
I want to say that the file may be too large, but that makes no sense whatsoever.
« Last Edit: December 15, 2012, 04:01:14 PM by Evar678 »

Try this.

Code: [Select]
function evar_getLineCount()
{
%lineCt = 0;
%file = new fileObject();
%file.openForRead("Add-Ons/Client_Derp/text.txt");
while(!%file.isEOF())
{
%lineCt++;
echo("Line" SPC %lineCt @ ":" SPC %file.readLine());
}
%file.close();
%file.delete();

return %lineCt;
}

Get rid of the last 3 lines of code, and add this to the end:

return %lineCt;
What are you thinking? He needs to close and delete the file object before leaving the function. Use Greek2me's code.

Use Greek2me's code.
That worked.

Thank you so much Greek2me
And also thanks everyone that helped. I should be able to handle the rest from here.

What are you thinking? He needs to close and delete the file object before leaving the function. Use Greek2me's code.
I could've sworn he wrote the close and delete twice, which is why I told him to replace the second pair.

I could've sworn he wrote the close and delete twice, which is why I told him to replace the second pair.
Accidentally copy+pasted that part twice I believe.
I took it out within 30 seconds of posting the topic