Liberty Unleashed

Scripting => Script Help => Topic started by: Vortrex on February 16, 2013, 05:25:16 am

Title: File Reading/Writing
Post by: Vortrex on February 16, 2013, 05:25:16 am
I have a quick question about file reading/writing. I cannot seem to figure out how to take a normal file and outputting its contents and other manipulations that I may need.

I downloaded a GeoLite database with some GeoIP information. It's in the form of a CSV (Comma Seperated Value) file. I know how to parse this using Squirrel, so I don't need any help there.

My question is, how do I output the file to a variable or something, so I can further process it? It's in the form of an instance.

I hope its not something that's right under my nose, but I'm assuming I'm just missing something simple.

Thanks in advance,
Vortrex
Title: Re: File Reading/Writing
Post by: Nihau on February 16, 2013, 08:28:13 am
Code: [Select]
function FileWrite(filename, text )
{
local file_text, file_handle;
file_text = text + "\r\n";
file_handle = file( filename, "a+" );

foreach ( idx, chr in text ){
file_handle.writen( chr, 's' ); }
}

Code: [Select]
FileWrite("Logs/Stats.txt", "hello");
Btw its edited Windlord `s script.
Title: Re: File Reading/Writing
Post by: Nihau on February 18, 2013, 05:03:12 pm
http://forum.iv-multiplayer.com/index.php?topic=5911.0 (http://forum.iv-multiplayer.com/index.php?topic=5911.0)
Code: [Select]
function FileRead(filename)
{
    local myText = "", file_handle = file(filename, "r+");
    while(!file_handle.eos()) {
          myText += file_handle.readn('s').tochar();  }
    return myText;
}

But this code cant read every file (file encoding should be UCS-2 LE w/o BOM).

In such situations, playing with  readn (http://www.squirrel-lang.org/doc/sqstdlib2.html#d0e159) type  might help.