Game Development Community

T3D 1.1 Final - FileObject OpenForRead Fails - w/suggest Fix(ish) - LOGGED (THREED-3158)

by Steve Acaster · in Torque 3D Professional · 10/09/2011 (5:20 pm) · 2 replies

T3D 1.1Final

win7

target:
FileObject I/O

issue:
Fileobject does not seem to read lines in a file

repeat:
create 2 functions and have them exec'd somewhere on startup

first, create the file
function fileObjectTest1()
{
// Create a file object for writing
%fileWrite = new FileObject();

// Open a file to write to, if it does not exist it will be created
%fileWrite.OpenForWrite("./test.txt");
// Write a line to the text files
%fileWrite.writeLine("READ. READ CODE. CODE");


// Close the text file
%fileWrite.close();

// Cleanup
%fileWrite.delete();
}

And then a function to read it:

function fileObjectTest2()
{
   %file = new FileObject();
   
   if ( %file.openForRead("./test.txt") ) 
   {
		
		while ( !%file.isEOF() ) 
		{
			%line = %file.readLine();
			echo(%line);
		}
		
		%file.close();
	}
	else
	{
	   echo("read failed");  
	}
   %file.delete();
}

Boot up T3D, from the main menu you should be able to get the console and type in :
fileObjectTest1()
fileObjecttest2()

Notice how it reads nothing, no line is echoed, and the fail warning comes up - but the file is there. Look in the new text file under game/test.txt and you'll see the line is in there too, so write is okay.

Suggest ... fix it!

edit:
See below

#1
10/09/2011 (6:08 pm)
Okay - it's a pathing issue.

("./test.txt"); /// BAD for reading
("test.txt"); /// GOOD for reading

Also the opinion of IRC states that the file should be expanded before being read ... something that doesn't happen in stock scripts.

function fileObjectTest2()
{
   %filename = expandFilename("test.txt");//game directory
   %file = new FileObject();
   
   if ( %file.openForRead(%filename) ) 
   {
		
		while ( !%file.isEOF() ) 
		{
			%line = %file.readLine();
			echo(%line);
		}
		
		%file.close();
	}
	else
	{
	   echo("read failed");  
	}
   %file.delete();
}

So it's expecting full paths. The TorqueScript Manual CHM should have the examples under FileObject updated to reflect this.
#2
01/11/2012 (12:17 pm)
Thanks Steve. I've gone ahead and logged this issue under ticket number THREED-3158 for review.