Author Topic: File I/O Tutorial  (Read 4365 times)

Since threads about these have recently been popping up, I thought I'd make a neat little tutorial about them.

Contents
-Introduction
-What is File I/O?
--Uses of File I/O
-Basic Use
-Writing files
-Reading files
-Miscellaneous Information

Introduction
In this tutorial, I'll be teaching you how to read and write files in Torquescript using fileObjects.



What is File I/O?
File I/O stands for "File Input/Output". This means you can read and write files using this system.
Uses of File I/O
File I/O can be used to read and write files. This is done through objects called "fileObjects". Some examples of when this is needed or could be used to make things easier:
  • Saving data and reading it after the server closes and opens back up.
  • Dumping data from objects.
  • Saving a list of words or values.



Basic Use
To start off, we need to know how to use them.

Code: [Select]
%file = new fileObject();
This creates a fileObject and the variable %file represents it in script.

To close the file after reading or writing, do this:

Code: [Select]
%file.close();
%file.delete();

This closes the file and deletes the fileObject.



Writing Files
To write a file, you use the functions openForWrite, which overwrites anything already in the file, and openForAppend, which simply adds onto a file.

Files should be specified like so:
<parent folder(s) seperated by forward slashes>/<filename>.<fileextension>

An example of openForWrite:

Code: [Select]
%file = new fileObject();
%file.openForWrite("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();

blah/blah.blah:
Quote
blah

You can do this as many times as you want and the file blah.blah will not change unless you change %file.writeLine("blah");.

An example of openForAppend:

Code: [Select]
%file = new fileObject();
%file.openForAppend("blah/blah.blah");
%file.writeLine("blah");
%file.close();
%file.delete();

After the last one, the file should now read:

Quote
blah
blah

If you do this more, it will add more lines containing whatever is in openForAppend.



Reading files
For reading files, there are a few functions you will most likely use.
openForRead - opens a file to read
isEOF - checks if we're at the end of the file or not.

These work together to do something like this:

Code: [Select]
%file = new fileObject();
%file.openForRead("blah/blah.blah");
while(!%file.isEOF())
{
  echo("Lines read:" SPC %lineCt);
  %lineCt++;
  echo(%file.readLine());
}
echo(%lineCt SPC "lines read total.");
%file.close();
%file.delete();

Basically, what this does is open our blah.blah file for read and as long as we haven't reached the end of the file, it echos how many lines we have read so far and echos the line. Once we reach the end of the file, it should tell us how many lines were read in all.

It would do something like this:

Quote
Lines read: 0
blah
Lines Read: 1
blah
1 lines read total.



Miscellaneous Information
This is a good reference for more information on File I/O.
http://docs.garagegames.com/tge/official/content/documentation/Reference/Console%20Functions/TorqueScript_Console_Functions_7.html

The add-on execution system works something like this:
Code: [Select]
function loadClientAddons()
{
for(%i = findFirstFile("Add-Ons/*/client.cs"); %i !$= ""; %i = findNextFile("Add-Ons/*/client.cs"))
{
echo("Loading: "@strReplace(strReplace(%i, "/client.cs", ""), "Add-Ons/",""));
exec(%i);
}
}
function loadServerAddons()
{
for(%i = findFirstFile("Add-Ons/*/server.cs");%i!$="";%i=findNextFile("Add-Ons/*/server.cs"))
{
echo("Loading: "@strReplace(strReplace(%i, "/server.cs",""), "Add-Ons/", ""));
exec(%i);
}
}
Using findFirstFile and findNextFile, it executes every single addon with the proper packaging.

The function save(object, file); can save data from an object to a file.




Hope this helps, if any questions or comments, say so.
« Last Edit: April 12, 2011, 04:54:12 PM by otto-san »

Looks like a very thorough tutorial, good job.

When you say %file.openForWrite("blah/blah.blah");, you should probably specify that it will go something like %file.openForWrite("Add-Ons/YourMod/server.cs");

And also you put two hyphens in the third item in the table of contents.  :cookieMonster:

Looks like a very thorough tutorial, good job.

When you say %file.openForWrite("blah/blah.blah");, you should probably specify that it will go something like %file.openForWrite("Add-Ons/YourMod/server.cs");

And also you put two hyphens in the third item in the table of contents.  :cookieMonster:
Yeah, I should. will do.

the two hyphens signal a subcategory. as does the fact that that one is not in size 15 print.

Yeah, I should. will do.

the two hyphens signal a subcategory. as does the fact that that one is not in size 15 print.
K
You could've just indented it, though.  :cookieMonster:

this actually looks pretty nice. i've never really learned how to do it.
for whatever reason, one time i tried it and it didn't work. however I have a feeling i know what i did:

Code: [Select]
%file.openForRead("config/client/database/database.cs");
%file.writeline(%name SPC %id);

hurr.


anyways, nice tutorial.

openForAppend
Thar she blows.

What is the best way for removing a line of data?

Thar she blows.

What is the best way for removing a line of data?
What exactly do you mean?

Thar she blows.

What is the best way for removing a line of data?
Read each line and store them into an array. Remove the line by removing the original line stored in the array, then loop through the array and write all the lines again.

Read each line and store them into an array. Remove the line by removing the original line stored in the array, then loop through the array and write all the lines again.

Gah thats awful.  If I have a text document that has thousands of lines, the only way to change one piece of information is by completely rebuilding the database?

Iban and you were right, using Fileobjects for saving and loading is hopeless.

I dont suppose you know how to get the time and date of a file? ... like what is displayed when you go to load a save?

I dont suppose you know how to get the time and date of a file? ... like what is displayed when you go to load a save?
Code: [Select]
getFileModifiedTime("config/client/prefs.cs")

actually, i didn't mention it in my first comment.
you define openForWrite well, but you give little to no description of openForAppend and from what you've given us, i can only see that it's the same as openForWrite?

actually, i didn't mention it in my first comment.
you define openForWrite well, but you give little to no description of openForAppend and from what you've given us, i can only see that it's the same as openForWrite?
"openForAppend, which simply adds onto a file."

"If you do this more, it will add more lines containing whatever is in openForAppend."

"After the last one, the file should now read:

'blah
blah'"

what

"openForAppend, which simply adds onto a file."

"If you do this more, it will add more lines containing whatever is in openForAppend."

"After the last one, the file should now read:

'blah
blah'"

what
i'm only seeing the last 2 bro.
that's all i needed though.
not sure if i'm missing something.

apparently theres something im not getting here, i keep getting the error:
unable to find function openForAppend
unable to find function newFileObject
???