View unanswered posts | View active topics It is currently Tue, September 25th, 2018, 12:03



Reply to topic  [ 5 posts ] 
Text File Functons. 
Author Message
User avatar

Posts: 76
BL Name: Fluffy
BL ID: 2992
Post Text File Functons.
Ok, I need the functions for creating a new file, adding a line, editing a line and reading a line.

Thanks in advance.

_________________
$Fluffy::Prefs::Annoyance= 1;


Fri, April 16th, 2010, 16:07
Profile
Global Moderator
User avatar

Posts: 650
BL Name: McTwist
BL ID: 9845
Post Re: Text File Functons.
I assume you're talking about TorqueScript. I may redirect you to the Torque Appendix, but instead, I'm telling it myself...

[b]Creating a file[/b]
[code]%stream = new FileObject();
%stream.openForWrite("myFile.txt");
%stream.close();
%stream.delete();[/code]

[b]Adding a line in a file[/b]
[code]%stream = new FileObject();
%stream.openForWrite("myFile.txt");
%stream.writeLine("Hi thar!");
%stream.close();
%stream.delete();[/code]

[b]Editing a line in a file[/b]
[code]%edittext = "My edit";
%editline = 12;
%stream = new FileObject();
%stream.openForRead("myFile.txt");
for (%i = 0; %$stream.isEOF(); %i++)
{
%lines[%i] = $stream.readLine();
}
%amountlines = %i
%stream.close();
%stream.delete();

%stream = new FileObject();
%stream.openForWrite("myFile.txt");
for (%i = 0; %i < %amountlines; %i++)
{
%line = (%i == %editline) ? %edittext : %lines[%i];
$stream.writeLine(%line);
}
%stream.close();
%stream.delete();[/code]

[b]Reading a line in a file[/b]
[code]%stream = new FileObject();
%stream.openForRead("myFile.txt");
%line = %stream.readLine();
%stream.close();
%stream.delete();[/code]

The editing is hard, and I wrote it by hand just recently, because there's few that actual want to do it due of how complex and inefficient it actual is.

_________________
[img]http://img115.imageshack.us/img115/2213/logoqn0zp3.png[/img]


Fri, April 16th, 2010, 17:25
Profile
User avatar

Posts: 76
BL Name: Fluffy
BL ID: 2992
Post Re: Text File Functons.
So can %Stream be any variable? such as %File ?

_________________
$Fluffy::Prefs::Annoyance= 1;


Fri, April 16th, 2010, 17:39
Profile
User avatar

Posts: 76
BL Name: Fluffy
BL ID: 2992
Post Re: Text File Functons.
Double Toast =/

Anyway, how do I check whether there is already a line containing data example: If I collect a list of clan tags (Not really) and I dont want the same one to be listed ore than once, how would I stop this?

_________________
$Fluffy::Prefs::Annoyance= 1;


Fri, April 16th, 2010, 20:11
Profile
Global Moderator
User avatar

Posts: 650
BL Name: McTwist
BL ID: 9845
Post Re: Text File Functons.
As a normal scripter would do, they would search through the list for the tag. I would check it through a hash table.

[code]%hash["tag"] = 4;
%list[4] = "hash";
%newTag = "tag";
if(%hash[%newTag] >= 0)
echo("Already exist");
else
{
%list[%amountList] = %newTag;
%hash[%newTag] = %amountList;
%amountList++;
}[/code]

If you meant through a file, then I suggest you check before you add the tag, or you just append the tag and when loading it you check it through the table.

_________________
[img]http://img115.imageshack.us/img115/2213/logoqn0zp3.png[/img]


Sat, April 17th, 2010, 13:43
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.