Game Development Community

Opening a text file

by Michael Smith · in Torque 2D Beginner · 06/13/2013 (5:00 pm) · 2 replies

I need to open a text file and parse its contents using string functions. I have the string functions working fine but I am not sure how to get a hold on the contents of the file. Can anyone suggest a method?

The file contains level information which is formatted consistently but is not XML. I can parse it if I can read it into string variables in chunks. I see that there are file functions in the global namespace but I am not familiar with how to use them.

About the author

Bible translator by day-- game programmer by night


#1
06/13/2013 (8:21 pm)
To read a file, you can use something like this:

// %file is the string of the path and file
function readFile(%file)
{
   %fo = new FileObject();
   if (%fo.openForRead(%file))
   {
      while (!%fo.isEOF())
      {
         %line = %fo.readLine();

         // do stuff here now, %line is your current line.
      }
      %fo.close();
   }
   %fo.delete();
}

Hope this helps :)
#2
06/13/2013 (9:31 pm)
Thanks, that is great!